[PyQt] cannot emit destroyed event

Phil Thompson phil at riverbankcomputing.com
Wed Jul 21 08:52:16 BST 2010


On Wed, 21 Jul 2010 15:32:33 +0800, 机械唯物主义 : linjunhalida
<linjunhalida at gmail.com> wrote:
> here is my code, and after close the window,
> stop method is not triggered as expected. what's wrong?
> windows + pyqt4.5.4
> 
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
> 
> class MyClass(QWidget):
>    def __init__(self):
>        QWidget.__init__(self)
>        self.destroyed.connect(self.stop)
> 
>    def stop(self, e=None):
>        print("stoped")
> 
> def main():
>    app = QApplication([])
>    c = MyClass()
>    c.show()
>    app.exec_()
> 
> if __name__=="__main__":
>    main()

Two problems...

You'll be lucky if the object can respond itself (ie. run the stop()
method) while it is being destroyed. Try connecting to a function instead.

The instance is being destroyed when the interpreter exits and after your
application has terminated. To destroy it while your application is running
add "del c" after the call to app.exec_().

Phil


More information about the PyQt mailing list