python - 为什么在接受 PyQt4 QDialog 时不会调用 closeEvent 和 destroyed slot?

标签 python qt qt4 pyqt4

全部在问题标题中,下面是我的简化代码。 根据文档: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qdialog.html#done 单击确定后,它应该关闭,实际上如果我使用窗口关闭按钮关闭它,它会触发事件:

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, *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_()

最佳答案

答案来自http://www.riverbankcomputing.com/pipermail/pyqt/2011-April/029589.html 非常感谢 Hans-Peter Jansen

  1. 不要将销毁信号的处理程序挂接到被销毁的对象上。(回想起来似乎很明显)

  2. QDialog.accept() 不会触发 closeEvent,即使它确实被销毁 - 恕我直言,错误或文档具有误导性

关于python - 为什么在接受 PyQt4 QDialog 时不会调用 closeEvent 和 destroyed slot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5570402/

相关文章:

python - 超时不工作 django-redis-cache

c++ - Qt5 QImageReader 奇怪的行为

c++ - QT 5.5 将外部应用程序嵌入到 QWidget 中

c++ - 在 Qt/C++ 中实现撤消功能(如 Ctrl+Z)

c++ - 使用 qmultimedia 低级 API 的 Qt 中的音频输出问题

python - 如何在 Pandas 中填充行不为 NULL 的列名?

python - findall 和正则表达式,得到正确的模式

python - 如何将父变量传递给python中的子进程?

c++ - Qt 应用程序适用于台式机,但不适用于笔记本电脑?

c++ - 通过 getter 获取时在其他类中声明结构变量