PyQt's QMainWindow closeEvent is never called

后端 未结 2 1954
说谎
说谎 2021-01-03 09:06

I\'m using pyqt4 in python.
When I close the QMainWindow window , its not handled by closeEvent method. Please someone tell me what is wrong with this cod

相关标签:
2条回答
  • 2021-01-03 09:38

    The closeEvent is a method of QMainwindow, but your trying to use it from you're own Ui_MainWindow class. Try this instead:

    from PyQt4 import QtCore, QtGui
    
    
    class MainWindow(QtGui.QMainWindow):
        def __init__(self, *args, **kwargs):
            super(MainWindow, self).__init__(*args, **kwargs)
            self.setupUi()
    
        def setupUi(self):
            self.setObjectName("MainWindow")
            self.resize(277, 244)
            self.statusbar = QtGui.QStatusBar()
            self.statusbar.setObjectName("statusbar")
            self.setStatusBar(self.statusbar)
    
        def closeEvent(self,event):
            result = QtGui.QMessageBox.question(self,
                          "Confirm Exit...",
                          "Are you sure you want to exit ?",
                          QtGui.QMessageBox.Yes| QtGui.QMessageBox.No)
            event.ignore()
    
            if result == QtGui.QMessageBox.Yes:
                event.accept()
    
    if __name__ == "__main__":
        import sys
        app = QtGui.QApplication(sys.argv)
        mainWindow = MainWindow()
        mainWindow.show()
        sys.exit(app.exec_())
    

    If you want to keep as much of your structure as possible, try this:

    from PyQt4 import QtCore, QtGui
    
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(277, 244)
            self.centralwidget = QtGui.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            MainWindow.setCentralWidget(self.centralwidget)
            self.statusbar = QtGui.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
            MainWindow.show()
    
    
    class MyWindow(QtGui.QMainWindow):
        def closeEvent(self,event):
            result = QtGui.QMessageBox.question(self,
                          "Confirm Exit...",
                          "Are you sure you want to exit ?",
                          QtGui.QMessageBox.Yes| QtGui.QMessageBox.No)
            event.ignore()
    
            if result == QtGui.QMessageBox.Yes:
                event.accept()
    
    
    if __name__ == "__main__":
        import sys
        app = QtGui.QApplication(sys.argv)
        MainWindow = MyWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        sys.exit(app.exec_())
    
    0 讨论(0)
  • 2021-01-03 09:57

    This is my solution,

        def closeEvent(self, e):
            close = QMessageBox.question(
                self,
                "Close Application",
                "Are you sure?",
                QMessageBox.Yes | QMessageBox.No,
                QMessageBox.No
            )
            e.ignore()
            if close == QMessageBox.Yes:
                e.accept()
    
    0 讨论(0)
提交回复
热议问题