PyQt: clicking X doesn't trigger closeEvent

Wookie88 picture Wookie88 · Feb 12, 2013 · Viewed 33.2k times · Source

I'm a total newbie in PyQt trying to develop simple application. I have designed simple ui with Qt-designer. I want extra confirmation if the user really want to exit application when clicking X or ,,Exit'' button or choosing Exit from menu.

Here's the code:

import sys
from PyQt4 import QtGui, QtCore, uic

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.ui = uic.loadUi('main_window.ui')
        self.ui.show()

        self.ui.btnExit.clicked.connect(self.close)
        self.ui.actionExit.triggered.connect(self.close)

    def closeEvent(self, event):
        print("event")
        reply = QtGui.QMessageBox.question(self, 'Message',
            "Are you sure to quit?", QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)

        if reply == QtGui.QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()


def main():
    app = QtGui.QApplication(sys.argv)
    win = MainWindow()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

The problem is that:

  • When I click X on main window the closeEvent function doesn't trigger
  • When I click Exit button or choose ,,Exit'' from menu, the function
    is called, but clicking Yes doesn't close application.

I have found some questions on SO and searched for tutorials, but nothing covered such problem. What am I doing wrong?

Answer

Avaris picture Avaris · Feb 12, 2013

Note that you're doing:

self.ui = uic.loadUi('main_window.ui')
self.ui.show()

Your actual window is an instance attribute (ui) inside win. Not the win itself. And it doesn't have closeEvent implemented.

loadUi can load the .ui file inside an instance.

PyQt4.uic.loadUi(uifile[, baseinstance=None[, package='']])

You should use that. With that, your code would be:

import sys
from PyQt4 import QtGui, QtCore, uic

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        uic.loadUi('main_window.ui', self)

        self.btnExit.clicked.connect(self.close)
        self.actionExit.triggered.connect(self.close)

    def closeEvent(self, event):
        print("event")
        reply = QtGui.QMessageBox.question(self, 'Message',
            "Are you sure to quit?", QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)

        if reply == QtGui.QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()


def main():
    app = QtGui.QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

Note: I'm not a fan of showing the window in __init__. Explicit is better. So, I moved that to main. Feel free to modify it.