2017-05-10 14 views
0

我是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对象的相同函数。如果这是正确的,我不知道如何管理这个。 在此先感谢您的帮助!

+0

您应该提供一个显示报告问题的[mcve](https://stackoverflow.com/help/mcve)。基于您的代码的最小示例可能看起来像[this](https://pastebin.com/jZPsR5e6),但不会显示您描述的行为。你确定你没有连接到'self.close'某处的附加信号吗? – mata

回答

0

谢谢你提示如何构建一个很好的代码示例。对不起,但我是新来的,然后我没有成功做出一个好榜样。但是,与此同时,我正在考虑这个问题,最后,我用一个小窍门解决了这个问题。

我已经添加了一个全局变量,用于计算是否关闭主窗口,然后在其他调用closeEvent()时避免调用测试过程。 我试着创建一个类,生成外部主类的QMessageBox,然后重写此对象的closeEvent(),但不起作用。我发现的唯一方法是全局变量。程序继续调用两次closeEvent(一个用于QMessageBox,另一个用于mainwindow),但现在,第二次忽略回忆测试。这是一个伎俩,它不是优雅的,但它适用于我。

的一段代码,现在是这样的:

#!/usr/bin/env python 
#-*- coding:utf-8 -*- 

import sys 
import os 
import datetime 
import inspect 
import csv 
import codecs 
import pymssql 
from PyQt4 import QtGui, QtCore, Qt 
import mainwindow 
#import _winreg only on Microsoft platforms 
import platform 
winos = True 
if os.name == 'nt' and platform.system() == 'Windows': 
    import _winreg 
    #other stuff needed under Windows 
    import _mssql 
    import decimal 
    import uuid 
    winos = True 
else: 
    winos = False 

#other global variables 
liccode = False 
closemain = False 


class MainWindow(QtGui.QMainWindow): 
    def __init__(self, parent=None): 
     QtGui.QDialog.__init__(self, parent) 
     super(MainWindow, self).__init__(parent) 
     self.ui = mainwindow.Ui_MainWindow() 
     self.ui.setupUi(self) 
     # some stuff and widgets to visualize in the main window 

     #connettors to menù functions; leave only the closing one 
     self.ui.actionChiudi.triggered.connect(self.close) 

    #override of closeEvent - there I think, it's better to work to separate 
    #closeEvent for mainwindow from general closeEvent. But how? 
    def closeEvent(self, event): 
     global closemain 
     #make evaluation test to decide how to work. Close directly or no? 
     if self.csvViewer.rowCount() > 0 and not closemain: 
      print event 
      #show a warning 
      Error = QtGui.QMessageBox() 
      Error.setIcon(QtGui.QMessageBox.Question) 
      Error.setWindowTitle('WARNING !!') 
      Error.setInformativeText(u"Are you sure to leave?") 
      Error.setStandardButtons(QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel) 
      ret = Error.exec_() 
      if ret == QtGui.QMessageBox.Ok: 
       closemain = True 
       event.accept() 
      else: 
       event.ignore() 
     else: 
      #close directly 
      event.accept() 
    #start application and create main 
    app = QtGui.QApplication(sys.argv) 
    my_mainWindow = MainWindow() 
    my_mainWindow.show() 
    sys.exit(app.exec_()) 

在任何情况下,任何的建议是广为接受,以做出更好的代码。谢谢你们!