我是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