我是Python和PyQt的新手。我试图在关闭主窗口之前管理closeEvent to ask,但这只适用于“X”按钮。从为询问用户而创建的QMEssageBox中,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对象调用相同的函数。如果这是对的,我不知道该怎么办。 提前感谢您的帮助!在
感谢您关于如何构造一个好的代码示例的提示。对不起,我是新来的,所以我没有成功地树立一个好榜样。但是,与此同时,我在思考这个问题,最后,我用一个小技巧解决了这个问题。
我添加了一个全局变量,如果您正在关闭主窗口,那么在其他调用closeEvent()时避免调用测试过程。 我试图在主类外部创建一个用于生成QMessageBox的类,然后重写该对象的closeEvent(),但没有起作用。我找到的唯一方法就是全局变量。程序继续调用两次closeEvent(一次用于QMessageBox,一次用于主窗口),但现在,第二次调用ignore来调用测试。这是个把戏,不优雅,但对我有用。
现在的代码是:
在任何情况下,任何建议都是可以接受的,以便编写更好的代码。谢谢大家!
相关问题 更多 >
编程相关推荐