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. form.ui editing has no effect
Forum Updated to NodeBB v4.3 + New Features

form.ui editing has no effect

Scheduled Pinned Locked Moved Unsolved Qt for Python
27 Posts 5 Posters 5.3k 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.
  • eyllanescE eyllanesc

    @Andy314 please share the .ui file

    Andy314A Offline
    Andy314A Offline
    Andy314
    wrote on last edited by
    #15

    @eyllanesc said in form.ui editing has no effect:

    @Andy314 please share the .ui file

    Here is it.

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>PySideTest2</class>
     <widget class="QMainWindow" name="PySideTest2">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>800</width>
        <height>600</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>PySideTest2</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <widget class="QPushButton" name="pushButton">
        <property name="geometry">
         <rect>
          <x>10</x>
          <y>10</y>
          <width>75</width>
          <height>23</height>
         </rect>
        </property>
        <property name="text">
         <string>PushButton</string>
        </property>
       </widget>
      </widget>
      <widget class="QMenuBar" name="menubar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>800</width>
         <height>21</height>
        </rect>
       </property>
       <widget class="QMenu" name="menuFile">
        <property name="title">
         <string>File</string>
        </property>
        <addaction name="actionTest"/>
       </widget>
       <widget class="QMenu" name="menuExit">
        <property name="title">
         <string>Exit</string>
        </property>
       </widget>
       <addaction name="menuFile"/>
       <addaction name="menuExit"/>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
      <action name="actionTest">
       <property name="text">
        <string>Test</string>
       </property>
      </action>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    eyllanescE 1 Reply Last reply
    0
    • Andy314A Andy314

      @eyllanesc said in form.ui editing has no effect:

      @Andy314 please share the .ui file

      Here is it.

      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
       <class>PySideTest2</class>
       <widget class="QMainWindow" name="PySideTest2">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>800</width>
          <height>600</height>
         </rect>
        </property>
        <property name="windowTitle">
         <string>PySideTest2</string>
        </property>
        <widget class="QWidget" name="centralwidget">
         <widget class="QPushButton" name="pushButton">
          <property name="geometry">
           <rect>
            <x>10</x>
            <y>10</y>
            <width>75</width>
            <height>23</height>
           </rect>
          </property>
          <property name="text">
           <string>PushButton</string>
          </property>
         </widget>
        </widget>
        <widget class="QMenuBar" name="menubar">
         <property name="geometry">
          <rect>
           <x>0</x>
           <y>0</y>
           <width>800</width>
           <height>21</height>
          </rect>
         </property>
         <widget class="QMenu" name="menuFile">
          <property name="title">
           <string>File</string>
          </property>
          <addaction name="actionTest"/>
         </widget>
         <widget class="QMenu" name="menuExit">
          <property name="title">
           <string>Exit</string>
          </property>
         </widget>
         <addaction name="menuFile"/>
         <addaction name="menuExit"/>
        </widget>
        <widget class="QStatusBar" name="statusbar"/>
        <action name="actionTest">
         <property name="text">
          <string>Test</string>
         </property>
        </action>
       </widget>
       <resources/>
       <connections/>
      </ui>
      
      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by
      #16

      @Andy314 QUiLoader does not populate a widget like PyQt5's loadUi does but instead creates a new instance, and that can be verified by modifying to the following:

          def load_ui(self):
              loader = QUiLoader()
              path = os.path.join(os.path.dirname(__file__), "form.ui")
              ui_file = QFile(path)
              ui_file.open(QFile.ReadOnly)
              widget = loader.load(ui_file, self)
              widget.show()
              ui_file.close()
      

      You will see that 2 windows are shown, one parent of the other but it will not be shown inside the window because the QMainWindow has the Qt::Window flag enabled by default. If you want to use QUiLoader then the class must behave like a controller:

      import sys
      import os
      
      from PySide2.QtWidgets import QApplication
      from PySide2.QtCore import QFile, QObject
      from PySide2.QtUiTools import QUiLoader
      
      
      class Controller(QObject):
          def __init__(self):
              super(Controller, self).__init__()
              self.load_ui()
              self.window.setWindowTitle("Qt Test")
      
          def load_ui(self):
              loader = QUiLoader()
              path = os.path.join(os.path.dirname(__file__), "form.ui")
              ui_file = QFile(path)
              ui_file.open(QFile.ReadOnly)
              self.window = loader.load(ui_file)
              ui_file.close()
      
          def show(self):
              self.window.show()
      
      
      if __name__ == "__main__":
          app = QApplication([])
          widget = Controller()
          widget.show()
          sys.exit(app.exec_())
      

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      Andy314A 1 Reply Last reply
      2
      • eyllanescE eyllanesc

        @Andy314 QUiLoader does not populate a widget like PyQt5's loadUi does but instead creates a new instance, and that can be verified by modifying to the following:

            def load_ui(self):
                loader = QUiLoader()
                path = os.path.join(os.path.dirname(__file__), "form.ui")
                ui_file = QFile(path)
                ui_file.open(QFile.ReadOnly)
                widget = loader.load(ui_file, self)
                widget.show()
                ui_file.close()
        

        You will see that 2 windows are shown, one parent of the other but it will not be shown inside the window because the QMainWindow has the Qt::Window flag enabled by default. If you want to use QUiLoader then the class must behave like a controller:

        import sys
        import os
        
        from PySide2.QtWidgets import QApplication
        from PySide2.QtCore import QFile, QObject
        from PySide2.QtUiTools import QUiLoader
        
        
        class Controller(QObject):
            def __init__(self):
                super(Controller, self).__init__()
                self.load_ui()
                self.window.setWindowTitle("Qt Test")
        
            def load_ui(self):
                loader = QUiLoader()
                path = os.path.join(os.path.dirname(__file__), "form.ui")
                ui_file = QFile(path)
                ui_file.open(QFile.ReadOnly)
                self.window = loader.load(ui_file)
                ui_file.close()
        
            def show(self):
                self.window.show()
        
        
        if __name__ == "__main__":
            app = QApplication([])
            widget = Controller()
            widget.show()
            sys.exit(app.exec_())
        
        Andy314A Offline
        Andy314A Offline
        Andy314
        wrote on last edited by
        #17

        @eyllanesc
        Wow, you are great ! Thank you very much for the help :-)

        With this tip, I found a good page of the problematic and want share it.
        https://www.learnpyqt.com/blog/pyqt5-vs-pyside2/

        If I understand it correct the problem is that the wizzard does not generate the correct code for the PySide modul. It generate from the code/class structure code for PyQt. But this code would not not run in PyQt because of the wrong uiloader function (uic.loadUi("mainwindow.ui", self should ist be).
        Its false for both modues.

        Hm, PyQt I "did not have on my screen."
        Should I maybe use PyQt instead of PySide.
        What is recommended ?

        eyllanescE 1 Reply Last reply
        0
        • Andy314A Andy314

          @eyllanesc
          Wow, you are great ! Thank you very much for the help :-)

          With this tip, I found a good page of the problematic and want share it.
          https://www.learnpyqt.com/blog/pyqt5-vs-pyside2/

          If I understand it correct the problem is that the wizzard does not generate the correct code for the PySide modul. It generate from the code/class structure code for PyQt. But this code would not not run in PyQt because of the wrong uiloader function (uic.loadUi("mainwindow.ui", self should ist be).
          Its false for both modues.

          Hm, PyQt I "did not have on my screen."
          Should I maybe use PyQt instead of PySide.
          What is recommended ?

          eyllanescE Offline
          eyllanescE Offline
          eyllanesc
          wrote on last edited by
          #18

          @Andy314 The problem is not the functions or libraries but your misunderstanding of what each function is for. In the case of loadUi if a second parameter is passed to it then it will take it as a toplevel unlike QUiLoader which does not. So the solution is: read in detail the docs of each function you use.

          If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

          Andy314A 1 Reply Last reply
          2
          • eyllanescE eyllanesc

            @Andy314 The problem is not the functions or libraries but your misunderstanding of what each function is for. In the case of loadUi if a second parameter is passed to it then it will take it as a toplevel unlike QUiLoader which does not. So the solution is: read in detail the docs of each function you use.

            Andy314A Offline
            Andy314A Offline
            Andy314
            wrote on last edited by Andy314
            #19

            @eyllanesc said in form.ui editing has no effect:

            @Andy314 The problem is not the functions or libraries but your misunderstanding of what each function is for. In the case of loadUi if a second parameter is passed to it then it will take it as a toplevel unlike QUiLoader which does not. So the solution is: read in detail the docs of each function you use.

            I come for C++ and thought it works just like that. I think a lot of PySide beginner struggle with this problem. I will check the docu and find a way.
            I am happy that we found a solution.

            JonBJ 1 Reply Last reply
            0
            • Andy314A Andy314

              @eyllanesc said in form.ui editing has no effect:

              @Andy314 The problem is not the functions or libraries but your misunderstanding of what each function is for. In the case of loadUi if a second parameter is passed to it then it will take it as a toplevel unlike QUiLoader which does not. So the solution is: read in detail the docs of each function you use.

              I come for C++ and thought it works just like that. I think a lot of PySide beginner struggle with this problem. I will check the docu and find a way.
              I am happy that we found a solution.

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

              @Andy314 said in form.ui editing has no effect:

              I come for C++ and thought it works just like that

              [Like what?] Then you will be pleased to see that PySide2's QUiLoader::load() is defined/works exactly like in C++, QWidget *QUiLoader::load(QIODevice *device, QWidget *parentWidget = nullptr).

              Which is quite different from PyQt5's PyQt5.uic.loadUi(), which works differently from C++....

              Andy314A 1 Reply Last reply
              0
              • JonBJ JonB

                @Andy314 said in form.ui editing has no effect:

                I come for C++ and thought it works just like that

                [Like what?] Then you will be pleased to see that PySide2's QUiLoader::load() is defined/works exactly like in C++, QWidget *QUiLoader::load(QIODevice *device, QWidget *parentWidget = nullptr).

                Which is quite different from PyQt5's PyQt5.uic.loadUi(), which works differently from C++....

                Andy314A Offline
                Andy314A Offline
                Andy314
                wrote on last edited by
                #21

                @JonB
                Ok, I have never worked with the QuiLoader directly I must admit. In C++ I use the wizard and the result works without doing anything more. The automatic generated ui->setupUi(this); makes all needed here all. I thought the wizard for Python did nearly the same - with whatever for code.

                JonBJ 1 Reply Last reply
                0
                • Andy314A Andy314

                  @JonB
                  Ok, I have never worked with the QuiLoader directly I must admit. In C++ I use the wizard and the result works without doing anything more. The automatic generated ui->setupUi(this); makes all needed here all. I thought the wizard for Python did nearly the same - with whatever for code.

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

                  @Andy314
                  I confess I'm not sure what you mean by "the wizard".

                  When I design for C++, Qt Designer produces a .ui file. And then --- whether you have noticed or not --- when you build one of the steps is to run the uic "compiler"/"preprocessor", which processes the .ui to produce a native C++ source file, which is actually what the C++ compiler compiles.

                  From C++, one could use QUiLoader, but I do not. I prefer to have auto-generated C++ class files, for a better coding-time experience.

                  Do you know why you are choosing to use the QUiLoader approach from Python/PySide2? I prefer the same approach as C++, getting an auto-generated Python/PySide2 source class file instead.

                  Andy314A 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @Andy314
                    I confess I'm not sure what you mean by "the wizard".

                    When I design for C++, Qt Designer produces a .ui file. And then --- whether you have noticed or not --- when you build one of the steps is to run the uic "compiler"/"preprocessor", which processes the .ui to produce a native C++ source file, which is actually what the C++ compiler compiles.

                    From C++, one could use QUiLoader, but I do not. I prefer to have auto-generated C++ class files, for a better coding-time experience.

                    Do you know why you are choosing to use the QUiLoader approach from Python/PySide2? I prefer the same approach as C++, getting an auto-generated Python/PySide2 source class file instead.

                    Andy314A Offline
                    Andy314A Offline
                    Andy314
                    wrote on last edited by Andy314
                    #23

                    @JonB said in form.ui editing has no effect:

                    @Andy314
                    I confess I'm not sure what you mean by "the wizard".

                    When I design for C++, Qt Designer produces a .ui file. And then --- whether you have noticed or not --- when you build one of the steps is to run the uic "compiler"/"preprocessor", which processes the .ui to produce a native C++ source file, which is actually what the C++ compiler compiles.

                    From C++, one could use QUiLoader, but I do not. I prefer to have auto-generated C++ class files, for a better coding-time experience.

                    Do you know why you are choosing to use the QUiLoader approach from Python/PySide2? I prefer the same approach as C++, getting an auto-generated Python/PySide2 source class file instead.

                    With wizzard I mean Menu:
                    File/New File or Project/ Application (Qt - C++)/ QtWidgets Application ....
                    resp.
                    File/New File or Project/ Application (Qt for Python)/ QtWidgets Application ....

                    The first works as expected. I can edit the Form with the designer and all works without an code changings. (automatic .ui compiling etc.)

                    For the second I expected the same - without changing of any code.
                    But the wizard generates the code shown above with using of the QUiLoader, what did not work as expected. That is not my code !

                    You say similar approach is with Python is possible - auto generated Python source classes. That would be great !
                    How can I achieve it?

                    JonBJ G 2 Replies Last reply
                    1
                    • Andy314A Andy314

                      @JonB said in form.ui editing has no effect:

                      @Andy314
                      I confess I'm not sure what you mean by "the wizard".

                      When I design for C++, Qt Designer produces a .ui file. And then --- whether you have noticed or not --- when you build one of the steps is to run the uic "compiler"/"preprocessor", which processes the .ui to produce a native C++ source file, which is actually what the C++ compiler compiles.

                      From C++, one could use QUiLoader, but I do not. I prefer to have auto-generated C++ class files, for a better coding-time experience.

                      Do you know why you are choosing to use the QUiLoader approach from Python/PySide2? I prefer the same approach as C++, getting an auto-generated Python/PySide2 source class file instead.

                      With wizzard I mean Menu:
                      File/New File or Project/ Application (Qt - C++)/ QtWidgets Application ....
                      resp.
                      File/New File or Project/ Application (Qt for Python)/ QtWidgets Application ....

                      The first works as expected. I can edit the Form with the designer and all works without an code changings. (automatic .ui compiling etc.)

                      For the second I expected the same - without changing of any code.
                      But the wizard generates the code shown above with using of the QUiLoader, what did not work as expected. That is not my code !

                      You say similar approach is with Python is possible - auto generated Python source classes. That would be great !
                      How can I achieve it?

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by JonB
                      #24

                      @Andy314
                      I'm going to keep this terse, because I don't use the same environment as you, so you will actually know better than I.

                      • I have never used any "wizard".
                      • I design stuff normally, let it save the .ui file.
                      • I then manually run the necessary command --- outside of Creator --- for producing .py file from .ui. For C++ that is uic. For PySide2 or for PyQt5, I think it's something like pyuic. They may be the same name as each other, but I think they are separate executables, each supplied with PySide2 or PyQt5. From C++ it happens as part of the build. For Python it's a pain to have to do it manually each time you change the .ui, but I still prefer that to run-time-only reading of the .ui file and creation of the widgets, because I get code-time support with a Python class.

                      This was a while ago. It may be now that with the integration of Python/PySide2 (to some extent) into Creator it does it for them, I do not know.

                      You say:

                      The first works as expected. I can edit the Form with the designer and all works without an code changings. (automatic .ui compiling etc.)

                      So it sounds like this is the thing, and is maybe auto-integrated now.

                      And so finally it sounds like, for whatever reason, they do not produce this for the "wizard" approach. I cannot think the "wizard" does much, maybe it's the same as without the wizard --- which then works for you --- just that the wizard asks some questions and then leaves you with some code.

                      So I suggest you either drop the wizard --- what is it doing for you anyway? --- or compare the project produced by the wizard to one without and just make a couple of adjustments to make it like the latter.

                      eyllanescE 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Andy314
                        I'm going to keep this terse, because I don't use the same environment as you, so you will actually know better than I.

                        • I have never used any "wizard".
                        • I design stuff normally, let it save the .ui file.
                        • I then manually run the necessary command --- outside of Creator --- for producing .py file from .ui. For C++ that is uic. For PySide2 or for PyQt5, I think it's something like pyuic. They may be the same name as each other, but I think they are separate executables, each supplied with PySide2 or PyQt5. From C++ it happens as part of the build. For Python it's a pain to have to do it manually each time you change the .ui, but I still prefer that to run-time-only reading of the .ui file and creation of the widgets, because I get code-time support with a Python class.

                        This was a while ago. It may be now that with the integration of Python/PySide2 (to some extent) into Creator it does it for them, I do not know.

                        You say:

                        The first works as expected. I can edit the Form with the designer and all works without an code changings. (automatic .ui compiling etc.)

                        So it sounds like this is the thing, and is maybe auto-integrated now.

                        And so finally it sounds like, for whatever reason, they do not produce this for the "wizard" approach. I cannot think the "wizard" does much, maybe it's the same as without the wizard --- which then works for you --- just that the wizard asks some questions and then leaves you with some code.

                        So I suggest you either drop the wizard --- what is it doing for you anyway? --- or compare the project produced by the wizard to one without and just make a couple of adjustments to make it like the latter.

                        eyllanescE Offline
                        eyllanescE Offline
                        eyllanesc
                        wrote on last edited by
                        #25

                        @JonB Previously there was PySide2-uic but now uic does that job:: uic -g python filename.ui -o filename_ui.py -x

                        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                        1 Reply Last reply
                        2
                        • Andy314A Andy314

                          @JonB said in form.ui editing has no effect:

                          @Andy314
                          I confess I'm not sure what you mean by "the wizard".

                          When I design for C++, Qt Designer produces a .ui file. And then --- whether you have noticed or not --- when you build one of the steps is to run the uic "compiler"/"preprocessor", which processes the .ui to produce a native C++ source file, which is actually what the C++ compiler compiles.

                          From C++, one could use QUiLoader, but I do not. I prefer to have auto-generated C++ class files, for a better coding-time experience.

                          Do you know why you are choosing to use the QUiLoader approach from Python/PySide2? I prefer the same approach as C++, getting an auto-generated Python/PySide2 source class file instead.

                          With wizzard I mean Menu:
                          File/New File or Project/ Application (Qt - C++)/ QtWidgets Application ....
                          resp.
                          File/New File or Project/ Application (Qt for Python)/ QtWidgets Application ....

                          The first works as expected. I can edit the Form with the designer and all works without an code changings. (automatic .ui compiling etc.)

                          For the second I expected the same - without changing of any code.
                          But the wizard generates the code shown above with using of the QUiLoader, what did not work as expected. That is not my code !

                          You say similar approach is with Python is possible - auto generated Python source classes. That would be great !
                          How can I achieve it?

                          G Offline
                          G Offline
                          GSTTROP
                          wrote on last edited by GSTTROP
                          #26

                          @Andy314 Hi I'm new to this forum but this is the only discussion I found about the problem I met with QT Creator when trying to simply create a QDialog window. The loader doesn't load anything. I m working with PyQt6 and PySide6. So on the last version of Qt Creator this problem still persists.
                          Thank's for the answer above which solved the problem.

                          import sys
                          import os

                          from PySide2.QtWidgets import QApplication
                          from PySide2.QtCore import QFile, QObject
                          from PySide2.QtUiTools import QUiLoader

                          class Controller(QObject):
                              def __init__(self):
                                  super(Controller, self).__init__()
                                  self.load_ui()
                                  self.window.setWindowTitle("Qt Test")
                          
                          def load_ui(self):
                              loader = QUiLoader()
                              path = os.path.join(os.path.dirname(__file__), "form.ui")
                              ui_file = QFile(path)
                              ui_file.open(QFile.ReadOnly)
                              self.window = loader.load(ui_file)
                              ui_file.close()
                          
                          def show(self):
                              self.window.show()
                          
                          if __name__ == "__main__":
                              app = QApplication([])
                              widget = Controller()
                              widget.show()
                              sys.exit(app.exec_())
                          `
                          jsulmJ 1 Reply Last reply
                          0
                          • G GSTTROP

                            @Andy314 Hi I'm new to this forum but this is the only discussion I found about the problem I met with QT Creator when trying to simply create a QDialog window. The loader doesn't load anything. I m working with PyQt6 and PySide6. So on the last version of Qt Creator this problem still persists.
                            Thank's for the answer above which solved the problem.

                            import sys
                            import os

                            from PySide2.QtWidgets import QApplication
                            from PySide2.QtCore import QFile, QObject
                            from PySide2.QtUiTools import QUiLoader

                            class Controller(QObject):
                                def __init__(self):
                                    super(Controller, self).__init__()
                                    self.load_ui()
                                    self.window.setWindowTitle("Qt Test")
                            
                            def load_ui(self):
                                loader = QUiLoader()
                                path = os.path.join(os.path.dirname(__file__), "form.ui")
                                ui_file = QFile(path)
                                ui_file.open(QFile.ReadOnly)
                                self.window = loader.load(ui_file)
                                ui_file.close()
                            
                            def show(self):
                                self.window.show()
                            
                            if __name__ == "__main__":
                                app = QApplication([])
                                widget = Controller()
                                widget.show()
                                sys.exit(app.exec_())
                            `
                            jsulmJ Online
                            jsulmJ Online
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #27

                            @GSTTROP said in form.ui editing has no effect:

                            path = os.path.join(os.path.dirname(file), "form.ui")

                            Did you verify that the path is correct?
                            What does ui_file.open(QFile.ReadOnly) return?

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            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