2017-05-10 13 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_()) 

在任何情況下,任何的建議是廣爲接受,以做出更好的代碼。謝謝你們!