Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Qt close event with Python issue

    Qt for Python
    2
    9
    7918
    Loading More Posts
    • 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.
    • Cobra91151
      Cobra91151 last edited by Cobra91151

      Hi! I want to intercept close event in my program. The problem when I press the X button it closes omitting close event. Nothing is printed to the console. Thanks in advance for your help.

      Code:

      class Ui_mainWindow(object):
          def closeEvent(self, event):
               event.ignore()
               print("Test...")
      
      1 Reply Last reply Reply Quote 0
      • Denni
        Denni last edited by Denni

        Note a mini fully working bit of example code is extremely helpful as it means someone trying to help you does not have to do it for you. As such here is a fully working bit of code that does what you say your code is not doing. I am using Python 3.7 / pyqt5 / Win10 and it works just fine

        from sys import exit as sysExit
        
        from PyQt5.QtCore import *
        from PyQt5.QtGui  import *
        from PyQt5.QtWidgets import *
        
        class CenterPanel(QWidget):
            def __init__(self, MainWin):
                QWidget.__init__(self)
        
                CenterPane = QHBoxLayout(self)
                CenterPane.addWidget(QTextEdit())
        
                self.setLayout(CenterPane)
                 
        class UI_MainWindow(QMainWindow):
            def __init__(self):
                super(UI_MainWindow, self).__init__()
        
                self.setCentralWidget(CenterPanel(self))
        
            def closeEvent(self, event):
                 print("Close Test 1")
        
        if __name__ == '__main__':
            MainApp = QApplication([])
        
            MainGui = UI_MainWindow()
            MainGui.show()
        
            sysExit(MainApp.exec_())
        

        madness... is like gravity, all takes is a little... push -- like from an unsolvable bug

        Cobra91151 1 Reply Last reply Reply Quote 0
        • Cobra91151
          Cobra91151 @Denni last edited by

          @Denni

          OK. Here is the code:

          main.py:

          rom PyQt5 import QtCore, QtGui, QtWidgets, Qt
          import sys
          from test1 import Ui_mainWindow
          
          if __name__ == "__main__":
              app = QtWidgets.QApplication(sys.argv)
              QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True);
              QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True);
              QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_UseOpenGLES, True);
              MainWindow = QtWidgets.QMainWindow()
              mainUI = Ui_mainWindow()
              mainUI.setupUi(MainWindow)
              MainWindow.show()
              sys.exit(app.exec_())
          

          test1.py

          class Ui_mainWindow(object):
             def closeEvent(self, event):
                  event.ignore()
                  print("Test...")
          
             def setupUi(self, mainWindow):
                 mainWindow.setObjectName("mainWindow")
                 mainWindow.resize(1895, 899)
                 mainWindow.setStyleSheet("")
                 mainWindow.setIconSize(QtCore.QSize(58, 48))
                 mainWindow.setDockNestingEnabled(True)
          
          1 Reply Last reply Reply Quote 0
          • Denni
            Denni last edited by Denni

            @Cobra91151 I had added to my previous post a bit of example code that does work perhaps that will help you ascertain where you problem resides

            madness... is like gravity, all takes is a little... push -- like from an unsolvable bug

            Cobra91151 1 Reply Last reply Reply Quote 0
            • Cobra91151
              Cobra91151 @Denni last edited by

              @Denni said in Qt close event with Python issue:

              I had added to my previous post a bit of example code that does work perhaps that will help you ascertain where you problem resides

              Ok. I will check it. Thanks.

              1 Reply Last reply Reply Quote 0
              • Denni
                Denni last edited by Denni

                Okay here is a working version of your program I think you can see where you might need to make adjustments to your code

                import sys
                
                from PyQt5.QtCore import *
                from PyQt5.QtGui  import *
                from PyQt5.QtWidgets import *
                
                class UI_MainWindow(QMainWindow):
                    def __init__(self):
                        super(UI_MainWindow, self).__init__()
                #class Ui_mainWindow(object):
                #   def setupUi(self, mainWindow):
                #       mainWindow.setObjectName("mainWindow")
                #       self.resize(1895, 899)
                        self.setStyleSheet("")
                        self.setIconSize(QSize(58, 48))
                        self.setDockNestingEnabled(True)
                
                    def closeEvent(self, event):
                #        event.ignore()
                        print("Test...")
                
                if __name__ == "__main__":
                    app = QApplication(sys.argv)
                    QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True);
                    QCoreApplication.setAttribute(Qt.AA_UseHighDpiPixmaps, True);
                    QCoreApplication.setAttribute(Qt.AA_UseOpenGLES, True);
                
                #    MainWindow = QMainWindow()
                #    mainUI = Ui_mainWindow()
                #    mainUI.setupUi(MainWindow)
                #    MainWindow.show()
                
                    MainGui = UI_MainWindow()
                    MainGui.show()
                
                    sys.exit(app.exec_())
                

                Note I removed the event.ignore because otherwise you have to basically crash the program to exit it and all you need to know is that you entered that function

                madness... is like gravity, all takes is a little... push -- like from an unsolvable bug

                Cobra91151 1 Reply Last reply Reply Quote 1
                • Cobra91151
                  Cobra91151 @Denni last edited by Cobra91151

                  @Denni

                  Thanks. I'm C++ developer, but this project should be done only with Python.
                  Also, the reason I want the closeEvent, because I want to stop loading page/free resources or do something to fix this QtWebEngine issue: QWaitCondition: Destroyed while threads are still waiting

                  I noticed this issue only occurs when page loading is in progress while I close the program.

                  1 Reply Last reply Reply Quote 0
                  • Cobra91151
                    Cobra91151 last edited by

                    The issue is resolved.

                    1 Reply Last reply Reply Quote 0
                    • Denni
                      Denni last edited by Denni

                      First @Cobra91151 your welcome and glad I could help. I would make a quick statement try to always K.I.S.S. your code (Keep It Simple and Smart) I have seen a lot of python and pyqt code recently that is overly complex (which is easy to do with python) due to not compartmentalizing the code as it should be.

                      For instance like in C++ code the main function should be reserved for what is needed within the main and only that -- aka for pyqt that is only the stuff that needs to address the Application object of the program and then only minimally so.

                      The GUI or whatever the Application object is running should be where you create your first Class this then isolates functionality and makes modifications and error handling/tracking so much easier. Happy coding in your project.

                      madness... is like gravity, all takes is a little... push -- like from an unsolvable bug

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post