我是Python和PyQt的新手。我试图管理closeEvent在关闭主窗口前询问,但是这只能从'X'按钮才能正常工作。从创建的QMEssageBox询问用户,callEvent()它被调用两次。Python和PyQt:在退出确认框callEvent()它被调用两次
这是代码的相关部分:
self.ui.actionChiudi.triggered.connect(self.close)
def closeEvent(self, event):
#check presence of data in the table
if self.csvViewer.rowCount() > 0:
print event # only to analyze the caller
#show a warning
Error = QtGui.QMessageBox()
Error.setIcon(QtGui.QMessageBox.Question)
Error.setWindowTitle('ATTENZIONE !!')
Error.setInformativeText(u"Sei sicuro di voler uscire?")
Error.setStandardButtons(QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel)
ret = Error.exec_()
if ret == QtGui.QMessageBox.Ok:
event.accept()
else:
event.ignore()
else:
#close directly
event.accept()
“actionChiudi”是在主菜单中的菜单项。 对于我所能理解的,当使用'X'按钮时,close()函数直接从mainwindow对象调用一次,然后关闭我的应用程序。 当使用menù项目时,函数创建新对象'QMessageBox',然后为该对象调用'closeEvent()'一次,然后调用mainwindow对象的相同函数。如果这是正确的,我不知道如何管理这个。 在此先感谢您的帮助!
您应该提供一个显示报告问题的[mcve](https://stackoverflow.com/help/mcve)。基于您的代码的最小示例可能看起来像[this](https://pastebin.com/jZPsR5e6),但不会显示您描述的行为。你确定你没有连接到'self.close'某处的附加信号吗? – mata