Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

form.ui editing has no effect



  • Hello,
    I am testing Python development with the QtCreator. With the wizzard I have created a project with QMainWindow. The main.py script starts indeed with the creating of the window - that works.

    With the Designer I put some menu entries widget on the window. The problem is that no changes will be shown in the running program. I get always a simple QMainWindow.

    What can be the problem ?

    Andreas


  • Lifetime Qt Champion

    @Andy314 How exactly do you use the ui file? Do you generate Python code from it or do you load it directly?



  • @jsulm said in form.ui editing has no effect:

    @Andy314 How exactly do you use the ui file? Do you generate Python code from it or do you load it directly?

    I took the original code that the wizzard has generated. I changed only the WindowTitle.

    from PySide2.QtWidgets import QApplication, QMainWindow
    from PySide2.QtCore import QFile
    from PySide2.QtUiTools import QUiLoader
    
    class TMainWindow(QMainWindow):
        def __init__(self):
            super(TMainWindow, self).__init__()
            self.load_ui()
            self.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)
            loader.load(ui_file, self)
            ui_file.close()
    
    
    if __name__ == "__main__":
        app = QApplication([])
        widget = TMainWindow()
        widget.show()
        sys.exit(app.exec_())
    

  • Lifetime Qt Champion

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

    ui_file.open(QFile.ReadOnly)

    You should check whether open succeeds. Maybe the file is not found?



  • @jsulm said in form.ui editing has no effect:

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

    ui_file.open(QFile.ReadOnly)

    You should check whether open succeeds. Maybe the file is not found?

        ```
    

    ok=ui_file.open(QFile.ReadOnly)

    gives True !

  • Lifetime Qt Champion

    @Andy314 Don't know. Are you sure you're really editing the file which is loaded at runtime?



  • @jsulm said in form.ui editing has no effect:

    @Andy314 Don't know. Are you sure you're really editing the file which is loaded at runtime?

    I am very sure, because I checked the path and the raw content of the ui-file.

            ```
            w=loader.load(ui_file, self)
            errorString=loader.errorString()
    
        
    errorString is empty ?

  • Lifetime Qt Champion

    @Andy314 Is

    os.path.join(os.path.dirname(__file__), "form.ui")
    

    the same path you're editing?



  • @jsulm said in form.ui editing has no effect:

    @Andy314 Is

    os.path.join(os.path.dirname(__file__), "form.ui")
    

    the same path you're editing?

    Yes ! This path had a mixing of \ / , what Qt usually doesn't bother.
    Nevetheless I wrote out the path explicit in the code, but it does help.

    I renamed the file in the folder. That gave an other error, so that I am sure that it is the right file.



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

    errorString is empty ?

    What tells you there has been any error? loader.errorString() will be empty if there is no error. You are supposed to check the return result to see if there is any error, before you try to look at errorString():

    w = loader.load(ui_file, self)
    if not w:
        errorString = loader.errorString()
        print(errorString)
        return
    


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

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

    errorString is empty ?

    What tells you there has been any error? loader.errorString() will be empty if there is no error. You are supposed to check the return result to see if there is any error, before you try to look at errorString():

    w = loader.load(ui_file, self)
    if not w:
        errorString = loader.errorString()
        print(errorString)
        return
    

    That was a quess of me. The return result is ok and not NULL (or how ever this is called in Python).



  • @Andy314
    Then you don't have any error on the loader.load(). I don't know whether that still leaves you with any other issue.



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

    @Andy314
    Then you don't have any error on the loader.load(). I don't know whether that still leaves you with any other issue.

    An chance to debug loader.load() ?

    Maybe its once again a versions problem as so often in Python as I found out.

    I use QtCreator 4.13.2 on Qt 5.15.1 (probably doesnt matter)
    and Python 3.8.7 64-bit with PySide 5.15.1.
    (with Python 3.9, PySide its doesnt work anyway because of shiboken dll missing.)

    Can some body give me a combination that is been in use and works, please ?
    (Creator I would not change.)



  • @Andy314 please share the .ui file



  • @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>
    


  • @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_())
    


  • @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 ?



  • @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.



  • @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.



  • @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++....



  • @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.



  • @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.



  • @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?



  • @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.



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



  • @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_())
    `

  • Lifetime Qt Champion

    @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?


Log in to reply