about how to destroy a QObject #110
Replies: 6 comments 1 reply
-
i found will destroy qObject |
BetaWas this translation helpful?Give feedback.
-
The object will be destroyed after the Python wrapper is no longer referenced (which happens in your example where you assign |
BetaWas this translation helpful?Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In fact, I am trying to dynamically load a script. file.py like this fromPythonQtimportQtCoreclassSomeClass(): def ▁▁init▁▁(self): print("SomeClass __init__") def ▁▁del▁▁(self): print("SomeClass __del__") someClass=SomeClass() classObjectManager(): def ▁▁init▁▁(self): self.qObject=QtCore.QLabel(parent) self.qObject.objectName="someObj"def ▁▁del▁▁(self): self.qObject.deleteLater() globalobjectManagerdefdoSome_slot(): passdefmain(init): globalobjectManagerif (_connect): objectManager=ObjectManager() objectManager.qObject.connect(objectManager.qObject., "some_signal()", doSome_slot) else: objectManager.qObject.disconnect(objectManager.qObject., "some_signal()", doSome_slot) objectManager=Nonec++ like this PythonQtObjectPtr Init(QObject* parent){PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut, QByteArray()); PythonQt_QtAll::init(); PythonQtObjectPtr module = PythonQt::self()->createModuleFromScript("ModuleName"); module.addObject("parent", parent); module.evalFile("file.py"); module.call("main", QVariantList() << true); returnmodule} voidFinalize(PythonQtObjectPtr module){module.call("main", QVariantList() << false); //will print "SomeClass __del__"_PyModule_Clear(module); PythonQtObjectPtr dict = PyImport_GetModuleDict(); PyDict_DelItemString(dict, "ModuleName")}if i not write deleteLater, the qObject will always remain in memory; |
BetaWas this translation helpful?Give feedback.
-
i getted so can write as "self.qObject.delete()" |
BetaWas this translation helpful?Give feedback.
-
This behaves as expected with Qt ownership rules - the parent object holds all if its children. To remove a child object you have to remove it from its parent instead of deleting it. If it is no longer held by another Qt object, or by a Python wrapper, it will be deleted. Deleting it yourself this way may even be dangerous. |
BetaWas this translation helpful?Give feedback.
-
i tried from PythonQt import QtCore, QtGui it will work fine |
BetaWas this translation helpful?Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
from PythonQt import QtCore
qObject = QtCore.QObject(parent)
i try to destroy qObject;
1.qObject = None
2.del qObject
3.qObject.deleteLater()
only 3 i found ~QObject Executed
anyone know the other way to destroy qObject?
BetaWas this translation helpful?Give feedback.
All reactions