Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. X button does not fire the closeEvent function
Qt 6.11 is out! See what's new in the release blog

X button does not fire the closeEvent function

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 3 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EyalT
    wrote on last edited by
    #1

    Hello

    I'm trying to use the closeEvent function when the user press X on main window but Python never fired closeEvent function.

    I read many posts about this issue but eventually I was not succeeded to resolve this.

    Any ideas why?

    Here is the code:

    import sys
    from PyQt5 import QtCore, QtGui, QtWidgets, uic
    from PyQt5.QtWidgets import QAction, QMessageBox, QMenu, QWidget

    class Ui_MainWindow(object):
    def init(self):
    super(Ui_MainWindow, self).init()
    print("Initialization succeeded")

    def closeEvent(self, event):
        print("closeEvent has been called")
        userResult = QMessageBox.question(None, "Quit", "Do you want to close the program?", QMessageBox.Yes,
                                          QMessageBox.No)
    
        if userResult == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()
    
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1920, 1080)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("Milvus UI", "Milvus UI"))
    

    if name == "main":
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

    JonBJ 1 Reply Last reply
    0
    • E EyalT

      Hello

      I'm trying to use the closeEvent function when the user press X on main window but Python never fired closeEvent function.

      I read many posts about this issue but eventually I was not succeeded to resolve this.

      Any ideas why?

      Here is the code:

      import sys
      from PyQt5 import QtCore, QtGui, QtWidgets, uic
      from PyQt5.QtWidgets import QAction, QMessageBox, QMenu, QWidget

      class Ui_MainWindow(object):
      def init(self):
      super(Ui_MainWindow, self).init()
      print("Initialization succeeded")

      def closeEvent(self, event):
          print("closeEvent has been called")
          userResult = QMessageBox.question(None, "Quit", "Do you want to close the program?", QMessageBox.Yes,
                                            QMessageBox.No)
      
          if userResult == QMessageBox.Yes:
              event.accept()
          else:
              event.ignore()
      
      def setupUi(self, MainWindow):
          MainWindow.setObjectName("MainWindow")
          MainWindow.resize(1920, 1080)
          self.retranslateUi(MainWindow)
          QtCore.QMetaObject.connectSlotsByName(MainWindow)
      
      def retranslateUi(self, MainWindow):
          _translate = QtCore.QCoreApplication.translate
          MainWindow.setWindowTitle(_translate("Milvus UI", "Milvus UI"))
      

      if name == "main":
      app = QtWidgets.QApplication(sys.argv)
      MainWindow = QtWidgets.QMainWindow()
      ui = Ui_MainWindow()
      ui.setupUi(MainWindow)
      MainWindow.show()
      sys.exit(app.exec_())

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @EyalT
      Please use the forum Code tags around blocks of code you paste.

      It looks like your def closeEvent() is a member of your Ui_MainWindow class. That is no good. It is an event method of QMainWindow and needs overriding. Put it in your MainWindow class instead.

      EDIT
      Oh, your MainWindow is a variable, not a class, and it's just directly a QMainWindow(). You will either need to create a derived class for it, or you may be able to achieve it via QObject.eventFilter().

      E 1 Reply Last reply
      1
      • JonBJ JonB

        @EyalT
        Please use the forum Code tags around blocks of code you paste.

        It looks like your def closeEvent() is a member of your Ui_MainWindow class. That is no good. It is an event method of QMainWindow and needs overriding. Put it in your MainWindow class instead.

        EDIT
        Oh, your MainWindow is a variable, not a class, and it's just directly a QMainWindow(). You will either need to create a derived class for it, or you may be able to achieve it via QObject.eventFilter().

        E Offline
        E Offline
        EyalT
        wrote on last edited by
        #3

        @JonB said in X button does not fire the closeEvent function:

        QObject.eventFilter().

        Thank you very much, I'll try and update.

        E 1 Reply Last reply
        0
        • E EyalT

          @JonB said in X button does not fire the closeEvent function:

          QObject.eventFilter().

          Thank you very much, I'll try and update.

          E Offline
          E Offline
          EyalT
          wrote on last edited by
          #4

          @EyalT I didn't succeeded and I would greatly appreciate it if you could make the necessary changes to the code I uploaded

          SGaistS 1 Reply Last reply
          0
          • E EyalT

            @EyalT I didn't succeeded and I would greatly appreciate it if you could make the necessary changes to the code I uploaded

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @EyalT hi,

            Create a subclass of QMainWindow, move the close event in it and then use it in your main function.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved