全部在问题标题中,下面是我的简化代码。 根据文档: 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
不要将销毁信号的处理程序挂接到被销毁的对象上。(回想起来似乎很明显)
QDialog.accept() 不会触发 closeEvent,即使它确实被销毁 - 恕我直言,错误或文档具有误导性
关于python - 为什么在接受 PyQt4 QDialog 时不会调用 closeEvent 和 destroyed slot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5570402/