Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Subclass QUiLoader
Forum Update on Monday, May 27th 2025

Subclass QUiLoader

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 2 Posters 1.1k Views
  • 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.
  • 3 Offline
    3 Offline
    3beezer
    wrote on last edited by 3beezer
    #1

    I am subclassing QUiLoader so that I can control the widgets created in createWidget. My code is something like this:

    from importlib import import_module
    
    class MyLoader(QUiLoader):
        def __init__(self):
            super().__init__()
            self.widget_mod = import_module("PySide2.QtWidgets")
    
        def createWidget(self, class_name, parent=None, name=''):
            orig_widget_inst = QUiLoader.createWidget(self, class_name, parent=None, name='')
            my_widget_inst = getattr(self.widget_mod, class_name)(parent)
            return my_widget_inst
    

    my_widget_inst appears to be equivalent to orig_widget_inst, but mine causes a segmentation fault. What else do I need to do to make my_widget_inst usable?

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

      Hi,

      @3beezer said in Subclass QUiLoader:

      def createWidget(self, class_name, parent=None, name=''):
      orig_widget_inst = QUiLoader.createWidget(self, class_name, parent=None, name='')
      widget_widget_inst = getattr(self.widget_mod, class_name)(parent)
      return my_widget_inst

      You are talking about my_widget_inst in your post but you don't have anything named like that created in your code. Is it just a typo ?

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

      3 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        @3beezer said in Subclass QUiLoader:

        def createWidget(self, class_name, parent=None, name=''):
        orig_widget_inst = QUiLoader.createWidget(self, class_name, parent=None, name='')
        widget_widget_inst = getattr(self.widget_mod, class_name)(parent)
        return my_widget_inst

        You are talking about my_widget_inst in your post but you don't have anything named like that created in your code. Is it just a typo ?

        3 Offline
        3 Offline
        3beezer
        wrote on last edited by
        #3

        @sgaist Typo. I fixed it. Sorry.

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

          Can you add a minimal sample code that uses this class and triggers the issue ?

          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
          • 3 Offline
            3 Offline
            3beezer
            wrote on last edited by
            #5

            test.py

            import sys
            from importlib import import_module
            
            from PySide2.QtCore import QFile
            from PySide2.QtUiTools import QUiLoader
            from PySide2.QtWidgets import QApplication
            
            class MyLoader(QUiLoader):
                def __init__(self):
                    super().__init__()
                    self.widget_mod = import_module("PySide2.QtWidgets")
            
                def createWidget(self, class_name, parent=None, name=''):
                    orig_widget_inst = QUiLoader.createWidget(self, class_name, parent=None, name='')
                    print("orig_widget_inst =", orig_widget_inst)
            
                    widget_class = getattr(self.widget_mod, class_name)
                    my_widget_inst = widget_class(parent)
                    print("my_widget_inst =", my_widget_inst)
            
                    return orig_widget_inst
                    #return my_widget_inst
            
            if __name__ == "__main__":
                app = QApplication(sys.argv)
                ui_file = QFile(sys.argv[1])
                ui_file.open(QFile.ReadOnly)
                loader = MyLoader()
                loader.load(ui_file)
                ui_file.close()
            

            and test.ui

            <?xml version="1.0" encoding="UTF-8"?>
            <ui version="4.0">
             <class>test_panel</class>
             <widget class="QWidget" name="test_panel"/>
             <resources/>
             <connections/>
            </ui>
            

            Run the program using

            python test.py test.ui
            

            Switch the comment on the two return statements to see the crash.

            1 Reply Last reply
            0
            • 3 Offline
              3 Offline
              3beezer
              wrote on last edited by
              #6

              I'm new here. Should I report this problem as a bug? If so, where?

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

                What version of PySide2 are you currently using ?
                On what OS ?

                PySide2 5.11.1 and 5.13.0 on macOS are working with your test example.

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

                3 1 Reply Last reply
                1
                • SGaistS SGaist

                  What version of PySide2 are you currently using ?
                  On what OS ?

                  PySide2 5.11.1 and 5.13.0 on macOS are working with your test example.

                  3 Offline
                  3 Offline
                  3beezer
                  wrote on last edited by
                  #8

                  @sgaist Interesting! I am using version 5.12.4 on Kubuntu 19.04 with Python 3.7.3. Oh, I see that I can upgrade PySide2 to 5.13.0 using pip. I will do so and report back.

                  1 Reply Last reply
                  0
                  • 3 Offline
                    3 Offline
                    3beezer
                    wrote on last edited by
                    #9

                    Nope. Still crashes. You did switch the # to the other return, right?

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

                      No I didn't because I overlooked it and I must say I expect to run crashing examples with the "working" version in comment rather than the contrary.

                      Before digging further, can you just explain what you want to do exactly in createWidget ?

                      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
                      • 3 Offline
                        3 Offline
                        3beezer
                        wrote on last edited by
                        #11

                        I would like to wrap the widget and return an instance of the wrapped widget. Since I can't return an instance of the exact same widget without getting a crash, it doesn't seem to be possible to override createWidget, despite claims to the contrary.

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

                          What wrapping do you have in mind ?

                          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
                          • 3 Offline
                            3 Offline
                            3beezer
                            wrote on last edited by
                            #13

                            I have several ideas. One I would have liked to implement earlier today is a subclass with __del__ so that I can confirm that I haven't created a cyclic reference. I do this in portions of the GUI I design manually, but I can't do it in portions I design using Qt Designer.

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

                              I wonder if it's a python specific issue.

                              Can you test if the same issue happens in C++ ?

                              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
                              • 3 Offline
                                3 Offline
                                3beezer
                                wrote on last edited by
                                #15

                                I think it is, but I don't know C++ well enough to write a test program.

                                1 Reply Last reply
                                0
                                • 3 Offline
                                  3 Offline
                                  3beezer
                                  wrote on last edited by
                                  #16

                                  The problem is officially a bug:

                                  bug 1070

                                  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