开发者

why do closeEvent and destroyed slot not get called on accepting PyQt4 QDialog?

All in the question title, and my simplified code below. According to the docs: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qdialog.html#done on clicking Ok, it should close and indeed if I close it using the window close button, it fires the event:

from PyQt4.QtGui import *
from PyQt4.QtCore import *

class MyDialog(QDialog):    
    def __init__(self):
        QDialog.__init__(self)
        self.button_box = QDialogButtonBox(self)
        self.button_box.addButton(self.button_box.Ok)
        self.connect(self.button_box, SIGNAL('accepted()'), self.on_accept)
        layout = QVBoxLayout()
        layout.addWidget(self.button_box)
        #self.setAttribute(Qt.WA_DeleteOnClose)
        self.setLayout(layout)
        self.connect(self.button_box, SIGNAL('destroyed(QObject*)'), self.on_destroyed)

    def on_destroyed(self,开发者_StackOverflow中文版 *args):
        print("destroying dialog")

    def on_accept(self):
        print("accepting")
        self.done(1)

    def closeEvent(self, event):
        print("close")
        return QDialog.closeEvent(self, event)

#    def __del__(self):
#        QDialog.destroy(self)

my_app = QApplication([])
my_widget = MyDialog()
result = my_widget.exec_()
del my_widget
#my_widget.destroy()
if result == 1:
    print("result!")
else:
    print("other result:", result)
my_app.exec_()


The answer came from http://www.riverbankcomputing.com/pipermail/pyqt/2011-April/029589.html with many thanks to Hans-Peter Jansen

  1. Don't hook handler for destroyed signal onto the object being destroyed.(seems obvious in retrospect)

  2. QDialog.accept() does not fire closeEvent, even though it does get destroyed - bug or documentation is misleading IMHO


That's not what the docs say at all:

As with QWidget.close(), done() deletes the dialog if the Qt.WA_DeleteOnClose flag is set.

It's not ever going to trigger a close event (It's not supposed to - you accepted rather than closing; these are two different things), and you've commented out the part that makes it delete the dialog.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜