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. closeEvent not running
Qt 6.11 is out! See what's new in the release blog

closeEvent not running

Scheduled Pinned Locked Moved Solved Qt for Python
36 Posts 3 Posters 12.8k Views 2 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #16

    You should learn how inheritance work.

    The proper way to add closeEvent handling to your Designer based class is to create a proper class i.e. you cannot use QUiLoader for that.

    Since you are using Python, you can cheat and monkey patch your window object (you really should name your variable in a more meaningful way).

    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
    1
    • D Offline
      D Offline
      DoubleFelix
      wrote on last edited by
      #17

      I do have a different name, I just named it differently for convenience. Although I guess it's less convenient than I thought.
      I think I understand how inheritance works, I just don't know how I'd make this a "proper" class while still loading from a UI. I guess I just don't know what I need to inherit to get closeEvent().

      JonBJ 1 Reply Last reply
      0
      • D DoubleFelix

        I do have a different name, I just named it differently for convenience. Although I guess it's less convenient than I thought.
        I think I understand how inheritance works, I just don't know how I'd make this a "proper" class while still loading from a UI. I guess I just don't know what I need to inherit to get closeEvent().

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

        @DoubleFelix
        I don't mean to open up a box of asps, but...

        Are you wedded to using QUiLoader/loadUiFile? This reads the .ui file generated from Qt Designer at runtime. It does not lend itself to subclassing, I think. And I believe that is what @SGaist is referring to

        The proper way to add closeEvent handling to your Designer based class is to create a proper class i.e. you cannot use QUiLoader for that.

        though I stand to be corrected.

        If that is so, read on:

        Most C++ Qt-ers will instead run the uic tool at compilation stage to generate the source code for a derived class, and use that in their project. This gives better control and compile-time help, IMHO.

        You can do the same from Python, if you wish. Exact depends on whether you are PyQt5 or PySide2/6, but principle is the same. They supply an equivalent pyuic tool, to read a .ui file and produce a Python class from it, which you then import.

        You can then subclass from that and define your own def closeEvent override.

        Read Option A: Generating a Python class. And compare against your way, which is Option B: Loading it directly. See which you prefer.

        Hope this helps.

        1 Reply Last reply
        1
        • D Offline
          D Offline
          DoubleFelix
          wrote on last edited by
          #19

          I wasn't aware that is what uic did. I will try that out! Is there a tool that can auto convert this stuff or do I need to do it with every change?

          JonBJ 1 Reply Last reply
          0
          • D DoubleFelix

            I wasn't aware that is what uic did. I will try that out! Is there a tool that can auto convert this stuff or do I need to do it with every change?

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

            @DoubleFelix
            You do have run the pyuic every time you re-save the .ui file. When using C++ inside Creator the build process will do that automatically just before recompiling. I don't know whether there is anything similar when you use Python/PySide inside Creator, or whether you have to do it by hand. You should Google around.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DoubleFelix
              wrote on last edited by
              #21

              Okay, I used pyuic5 -x window.ui -o window.py in the same directory as my window.py, but it gives me

              Fatal error in launcher: Unable to create process using '"d:\python\python.exe"  "D:\Python\Scripts\pyuic5.exe" -x window.ui -o window.py': The system cannot find the file specified.
              

              I'm not sure why it can't find the file. It doesn't work if I provide a direct file path either.

              JonBJ 1 Reply Last reply
              0
              • D DoubleFelix

                Okay, I used pyuic5 -x window.ui -o window.py in the same directory as my window.py, but it gives me

                Fatal error in launcher: Unable to create process using '"d:\python\python.exe"  "D:\Python\Scripts\pyuic5.exe" -x window.ui -o window.py': The system cannot find the file specified.
                

                I'm not sure why it can't find the file. It doesn't work if I provide a direct file path either.

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

                @DoubleFelix
                I don't know. But have a Google for, say, pyuic5 Fatal error in launcher. I'm getting a few hits for you to look through. I think one possible suggestion was make sure everything is either 32- or 64-bit?

                I think the pyuic on your PATH is a pyuic.bat, which runs python.exe to execute the pyuic5.exe. Try just running D:\Python\Scripts\pyuic5.exe directly and see whether you get an error from that?

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  DoubleFelix
                  wrote on last edited by DoubleFelix
                  #23

                  I can only find an instance of pyuic5.bat in an anaconda folder, which I do not use. I was originally running pyuic5.exe, which returns the error I'm getting. Sorry for not elaborating better. As for 32-bit vs. 64-bit, I have verified everything is 64-bit.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #24

                    How did you install PySide2 exactly ?

                    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
                    0
                    • D Offline
                      D Offline
                      DoubleFelix
                      wrote on last edited by
                      #25

                      With pip install pyside6 (I use PySide6 but I was told they were the same but with more features so I'm not sure it matters)

                      JonBJ 1 Reply Last reply
                      0
                      • D DoubleFelix

                        With pip install pyside6 (I use PySide6 but I was told they were the same but with more features so I'm not sure it matters)

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

                        @DoubleFelix
                        PySide6 is not the same as PySide2, hence the 6 instead of 2! It's PySide for Qt6, instead of for Qt5. It's new. It does not yet have all features either.

                        And I notice you are running pyuic5, which I assume from the 5 is for Qt5.

                        Anyway, if it were me, I would scrub (uninstall, remove) anything Qt6/PySide6, and install Qt 5.15-ish (latest) + PySide2. (If the PySide2 comes with its own version of Qt5 [I don't know if that's how it works] that's fine, you don't need to do Qt5 separately.) And try again, at least to see how that fares in comparison.

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #27

                          Is it your system pip ? Conda ?

                          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
                          0
                          • D Offline
                            D Offline
                            DoubleFelix
                            wrote on last edited by DoubleFelix
                            #28

                            @JonB I have uninstalled PySide6 and installed PySide2 and I get the same error.
                            @SGaist I don't think it is but I'm not sure how to tell

                            I attempted to use a mix of PySide6's normal features with PyQt5's uic module, but using

                            class TexasHoldEmServerWindow(QMainWindow):
                                def __init__(self):
                                    super(TexasHoldEmServerWindow, self).__init__()
                                    uic.loadUi(os.path.join(os.path.dirname(__file__), "window.ui"), self)
                            

                            Raises this error:

                            Exception has occurred: TypeError
                            ('Wrong base class of toplevel widget', (<class 'topaz.server.texas_hold_em.server_window.TexasHoldEmServerWindow'>, 'QMainWindow'))
                            
                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #29

                              Isn't the original base widget a QWidget ?

                              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
                              0
                              • D Offline
                                D Offline
                                DoubleFelix
                                wrote on last edited by
                                #30

                                I tried using that too but it throws the same error

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #31

                                  Then what base class did you select in designer ?

                                  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
                                  0
                                  • D Offline
                                    D Offline
                                    DoubleFelix
                                    wrote on last edited by DoubleFelix
                                    #32

                                    The main window is QMainWindow but I have a layout inside the window (grid) which is QWidget. Inheriting either gives the same error however.

                                    1 Reply Last reply
                                    0
                                    • D Offline
                                      D Offline
                                      DoubleFelix
                                      wrote on last edited by DoubleFelix
                                      #33

                                      I was able to get it working by using pyuic6 window.ui -o window.py
                                      I have this:

                                              super(TexasHoldEmServerWindow, self).__init__()
                                              self.window = Ui_MainWindow
                                              self.window.setupUi(self)
                                      

                                      But setupUi() also needs to passed a parameter called MainWindow, but I'm not sure what to give it here.

                                      1 Reply Last reply
                                      0
                                      • D Offline
                                        D Offline
                                        DoubleFelix
                                        wrote on last edited by DoubleFelix
                                        #34

                                        I got it sort of working using a bit of code someone put in a stack overflow post:

                                        from PySide6.QtCore import QMetaObject
                                        from PySide6.QtUiTools import QUiLoader
                                        
                                        class UiLoader(QUiLoader):
                                            def __init__(self, baseinstance, customWidgets=None):
                                                QUiLoader.__init__(self, baseinstance)
                                                self.baseinstance = baseinstance
                                                self.customWidgets = customWidgets
                                        
                                            def createWidget(self, class_name, parent=None, name=''):
                                                if parent is None and self.baseinstance:
                                                    return self.baseinstance
                                                else:
                                                    if class_name in self.availableWidgets():
                                                        widget = QUiLoader.createWidget(self, class_name, parent, name)
                                                    else:
                                                        try:
                                                            widget = self.customWidgets[class_name](parent)
                                                        except (TypeError, KeyError) as e:
                                                            #raise Exception('No custom widget ' + class_name + ' found in customWidgets param of UiLoader __init__.')
                                                            pass
                                                    if self.baseinstance:
                                                        setattr(self.baseinstance, name, widget)
                                                    return widget
                                        
                                        def loadUi(uifile, baseinstance=None, customWidgets=None,
                                                   workingDirectory=None):
                                        
                                            loader = UiLoader(baseinstance, customWidgets)
                                        
                                            if workingDirectory is not None:
                                                loader.setWorkingDirectory(workingDirectory)
                                        
                                            widget = loader.load(uifile)
                                            QMetaObject.connectSlotsByName(widget)
                                            return widget
                                        

                                        Then I add this to my UI Class:

                                                QMainWindow.__init__(self)
                                                loadUi(os.path.join(os.path.dirname(__file__), "window.ui"), self)
                                        

                                        But it keeps saying the widget creation fails.

                                        1 Reply Last reply
                                        0
                                        • D Offline
                                          D Offline
                                          DoubleFelix
                                          wrote on last edited by
                                          #35

                                          Wait, this is related to another issue. This one is solved.
                                          Sorry for being such a pain and thank you so much.

                                          I don't know how to close this

                                          SGaistS 1 Reply Last reply
                                          0

                                          • Login

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