Python 使用父窗口的closeevent关闭pyqt messageBox

Python 使用父窗口的closeevent关闭pyqt messageBox,python,pyqt,qmessagebox,Python,Pyqt,Qmessagebox,我有一块蛋糕 def __init__(): self._taskInProgress = threading.Event() def isFinished(self): self._taskInProgress.clear() self.progressBar.hide() self.close() def closeEvent(self, event): if self._taskInProgress.is_set(): rep

我有一块蛋糕

def __init__():
    self._taskInProgress = threading.Event()


def isFinished(self):
    self._taskInProgress.clear()
    self.progressBar.hide()
    self.close()


def closeEvent(self, event):
    if self._taskInProgress.is_set():
        reply = QtGui.QMessageBox.question(self, "Are you sure you want to quit? ",
            "Task is in progress !",
            QtGui.QMessageBox.Yes,
            QtGui.QMessageBox.No)
        if reply == QtGui.QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()
问题是,如果有人关闭父窗口(即self),则会显示上述提示,但如果有人未在此消息框中按yes或no,则父窗口不会关闭


那个么,当任务完成时,我应该如何实现呢?
QMessageBox
(即reply)也被iteslef关闭,就像调用
reply.close()

另一种方法,就是通过不按窗口中的X按钮来调用close小部件。(在本例中是call
isFinished
)。我们可以重写is close方法并向控件添加标志。像这样,

import sys
from PyQt4 import QtCore, QtGui

class QCsMainWindow (QtGui.QMainWindow):
    def __init__ (self):
        super(QCsMainWindow, self).__init__()
        self.isDirectlyClose = False
        QtCore.QTimer.singleShot(5 * 1000, self.close) # For simulate test direct close 

    def close (self):
        for childQWidget in self.findChildren(QtGui.QWidget):
            childQWidget.close()
        self.isDirectlyClose = True
        return QtGui.QMainWindow.close(self)

    def closeEvent (self, eventQCloseEvent):
        if self.isDirectlyClose:
            eventQCloseEvent.accept()
        else:
            answer = QtGui.QMessageBox.question (
                self,
                'Are you sure you want to quit ?',
                'Task is in progress !',
                QtGui.QMessageBox.Yes,
                QtGui.QMessageBox.No)
            if (answer == QtGui.QMessageBox.Yes) or (self.isDirectlyClose == True):
                eventQCloseEvent.accept()
            else:
                eventQCloseEvent.ignore()

appQApplication = QtGui.QApplication(sys.argv)
mainQWidget = QCsMainWindow()
mainQWidget.show()
sys.exit(appQApplication.exec_())