about how to destroy a QObject #110
Replies: 6 comments 1 reply
-
i found will destroy qObject |
Beta Was 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 |
Beta Was this translation helpful? Give feedback.
-
In fact, I am trying to dynamically load a script. file.py like this from PythonQt import QtCore
class SomeClass():
def ▁▁init▁▁(self):
print("SomeClass __init__")
def ▁▁del▁▁(self):
print("SomeClass __del__")
someClass = SomeClass()
class ObjectManager():
def ▁▁init▁▁(self):
self.qObject = QtCore.QLabel(parent)
self.qObject.objectName = "someObj"
def ▁▁del▁▁(self):
self.qObject.deleteLater()
global objectManager
def doSome_slot():
pass
def main(init):
global objectManager
if (_connect):
objectManager = ObjectManager()
objectManager.qObject.connect(objectManager.qObject., "some_signal()", doSome_slot)
else:
objectManager.qObject.disconnect(objectManager.qObject., "some_signal()", doSome_slot)
objectManager = None c++ 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);
return module;
}
void Finalize(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; |
Beta Was this translation helpful? Give feedback.
-
i getted so can write as "self.qObject.delete()" |
Beta Was 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. |
Beta Was this translation helpful? Give feedback.
-
i tried from PythonQt import QtCore, QtGui it will work fine |
Beta Was this translation helpful? Give feedback.
-
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?
Beta Was this translation helpful? Give feedback.
All reactions