I have a widget which would have to do some manual cleanup after it\'s destroyed (stop some threads). However for some reason the \"destroyed\" signal of the widget is not f
After a few tries I found out that it works if you declare the (see at the bottom)doSomeDestruction
outside the class.
But I don't know why. As written in this answer, this is because At the point destroyed() is emitted, the widget isn't a QWidget anymore, just a QObject (as destroyed() is emitted from ~QObject)
.
This means when your function would be called it is already deleted if you write it in the class. (look also here in the qt-interest mailing list: Ok , I am sorry for stupid question. The signal is emitted, but the slot is not called for the obvious reason, that is because object is already deleted.
)
EDIT: I've found two ways make it really work:
del window
after ret = app.exec_()
.Set the WA_DeleteOnClose
attribute in the main window (not the widget):
At the top of the program:
from PyQt4 import QtCore
The changed __init__
function:
class MyWindow(QtGui.QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
self.widget = MyWidget(self)