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. Qt Creator and Pyside2 - Blank Window
Forum Updated to NodeBB v4.3 + New Features

Qt Creator and Pyside2 - Blank Window

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

    Hi,

    Can you share the UI code as well ?

    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
    • M Offline
      M Offline
      Marcus Adamski
      wrote on last edited by
      #7

      Sure, the following is the UI for QWidget.

      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
       <class>main_window</class>
       <widget class="QWidget" name="main_window">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>800</width>
          <height>600</height>
         </rect>
        </property>
        <property name="windowTitle">
         <string>main_window</string>
        </property>
        <widget class="QPushButton" name="pushButton">
         <property name="geometry">
          <rect>
           <x>40</x>
           <y>40</y>
           <width>75</width>
           <height>23</height>
          </rect>
         </property>
         <property name="text">
          <string>PushButton</string>
         </property>
        </widget>
       </widget>
       <resources/>
       <connections/>
      </ui>
      

      ... and for the QMainApplication:

      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
       <class>main_window</class>
       <widget class="QMainWindow" name="main_window">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>800</width>
          <height>600</height>
         </rect>
        </property>
        <property name="windowTitle">
         <string>main_window</string>
        </property>
        <widget class="QWidget" name="centralwidget">
         <widget class="QPushButton" name="pushButton">
          <property name="geometry">
           <rect>
            <x>70</x>
            <y>80</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>
        <widget class="QStatusBar" name="statusbar"/>
       </widget>
       <resources/>
       <connections/>
      </ui>
      

      On both projects, in the above UIs the only change is the addition of a single pushbutton to identify the window

      JonBJ 1 Reply Last reply
      0
      • M Offline
        M Offline
        Marcus Adamski
        wrote on last edited by Marcus Adamski
        #8

        Actually, for QMainWindow, I don't think PySide2 allows the UI file to be loaded from within the constructor.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Marcus Adamski
          wrote on last edited by
          #9

          Thanks @Denni-0 for the valuable input. It's really helpful to have a solid template from which to explore the rest of Qt. I planned to use Qt Designer to get the feel of how the Qt framework behaves, then start coding the UI by hand. Right - next step, become more familiar with the Qt layouts

          Thanks again

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Marcus Adamski
            wrote on last edited by
            #10

            Just ran your code - initially got the error "'MainWindow' object has no attribute 'SetStatusBar'".
            However, after commenting out the following lines:

                 #self.StatBar = self.statusBar()
                 #self.SetStatusBar('Ready')
            

            and running the code, unfortunately a single window is displayed with the title "Da Pusher" .... but there's no button, just a blank window.

            window.jpg

            Have to pop out now - will take another look at the code this evening.

            Just out of interested, why does QWidget check:

            if self.Parent == None:
            

            is this so the QWidget can also run as an individual window if needed?

            Thanks

            JonBJ 1 Reply Last reply
            0
            • M Marcus Adamski

              Just ran your code - initially got the error "'MainWindow' object has no attribute 'SetStatusBar'".
              However, after commenting out the following lines:

                   #self.StatBar = self.statusBar()
                   #self.SetStatusBar('Ready')
              

              and running the code, unfortunately a single window is displayed with the title "Da Pusher" .... but there's no button, just a blank window.

              window.jpg

              Have to pop out now - will take another look at the code this evening.

              Just out of interested, why does QWidget check:

              if self.Parent == None:
              

              is this so the QWidget can also run as an individual window if needed?

              Thanks

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

              @Marcus-Adamski
              In the code you have been given there are a couple of mistakes.

              Apart from the self.SetStatusBar you noted, the problem you see stems from:

                      VBox = QVBoxLayout()
                      HBox.addStretch(1)
                      HBox.addLayout(HBox)
                      HBox.addStretch(1)
              

              This gives a warning "QLayout: Cannot add layout QHBoxLayout/ to itself", and under Linux dumps core on exit from the app.

              If you correct this to:

                      VBox = QVBoxLayout()
                      VBox.addStretch(1)
                      VBox.addLayout(HBox)
                      VBox.addStretch(1)
              

              the problems go away, and you will see the push button.

              1 Reply Last reply
              1
              • M Marcus Adamski

                Sure, the following is the UI for QWidget.

                <?xml version="1.0" encoding="UTF-8"?>
                <ui version="4.0">
                 <class>main_window</class>
                 <widget class="QWidget" name="main_window">
                  <property name="geometry">
                   <rect>
                    <x>0</x>
                    <y>0</y>
                    <width>800</width>
                    <height>600</height>
                   </rect>
                  </property>
                  <property name="windowTitle">
                   <string>main_window</string>
                  </property>
                  <widget class="QPushButton" name="pushButton">
                   <property name="geometry">
                    <rect>
                     <x>40</x>
                     <y>40</y>
                     <width>75</width>
                     <height>23</height>
                    </rect>
                   </property>
                   <property name="text">
                    <string>PushButton</string>
                   </property>
                  </widget>
                 </widget>
                 <resources/>
                 <connections/>
                </ui>
                

                ... and for the QMainApplication:

                <?xml version="1.0" encoding="UTF-8"?>
                <ui version="4.0">
                 <class>main_window</class>
                 <widget class="QMainWindow" name="main_window">
                  <property name="geometry">
                   <rect>
                    <x>0</x>
                    <y>0</y>
                    <width>800</width>
                    <height>600</height>
                   </rect>
                  </property>
                  <property name="windowTitle">
                   <string>main_window</string>
                  </property>
                  <widget class="QWidget" name="centralwidget">
                   <widget class="QPushButton" name="pushButton">
                    <property name="geometry">
                     <rect>
                      <x>70</x>
                      <y>80</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>
                  <widget class="QStatusBar" name="statusbar"/>
                 </widget>
                 <resources/>
                 <connections/>
                </ui>
                

                On both projects, in the above UIs the only change is the addition of a single pushbutton to identify the window

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

                @Marcus-Adamski
                Now going all the way back to your original Designer UI files, and why your code works with a QWidget but not with a QMainWindow.

                In your QMainWindow .ui file you have:

                  <widget class="QWidget" name="centralwidget">
                   <widget class="QPushButton" name="pushButton">
                

                This is not right, and Designer will be showing you a warning "no-entry" marker on centralwidget in the pane where it shows your widget hierarchy. You ignore this at your peril! Layout will "go wrong" if you make a QWidget a direct child on another (non-container) QWidget such as QMainWindow.centralWidget without you putting in a layout. The same would happen if you did this in your own code, though you wouldn't get any warning.

                Go back into Designer and give your central widget a layout: in the widget hierarchy pane, right-click on main_window, pick Lay out, pick Lay Out Horizontally.

                Now, to make it work in the QMainWindow case: I originally told you:

                @Marcus-Adamski
                PySide 2 QUiLoader().load() returns a widget you need to show().
                https://stackoverflow.com/questions/50128293/pyside2-quiloader-returns-an-empty-window

                but you did not act on this. You need to change two lines:

                loader.load(ui_file, self).show()
                # widget.show()
                

                So, I have made the return result from loader.load(ui_file, self) be the widget to call show() on, and got rid of your show() on the object constructed via main_window(). Once you see that works you can tidy the code to do this in a better way.

                You should get the push button visible as you do in the plain QWidget-non-QMainWindow case. You can then continue to use the Designer for your application if you wish to, as many do.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Marcus Adamski
                  wrote on last edited by
                  #13

                  Thanks to both @JonB and @Denni-0 , I now have a solid template (and better understanding) to explore QT further. Much appreciated.

                  1 Reply Last reply
                  0
                  • ErriezE Offline
                    ErriezE Offline
                    Erriez
                    wrote on last edited by Erriez
                    #14

                    I had the same problem with PySide6 on Ubuntu 22.04 and Windows 10: Creating a new project with QMainWindow does not work. See my question on StackOverflow.

                    Complete fixed example:

                    # This Python file uses the following encoding: utf-8
                    import os
                    from pathlib import Path
                    import sys
                    
                    from PySide6.QtWidgets import QApplication, QMainWindow
                    from PySide6.QtCore import QFile
                    from PySide6.QtUiTools import QUiLoader
                    
                    
                    class MainWindow(QMainWindow):
                        def __init__(self, parent=None):
                            super().__init__(parent)
                            self.load_ui()
                    
                        def load_ui(self):
                            loader = QUiLoader()
                            path = Path(__file__).resolve().parent / "form.ui"
                            ui_file = QFile(path)
                            ui_file.open(QFile.ReadOnly)
                    
                    -        loader.load(ui_file, self)
                    +        widget = loader.load(ui_file, self)
                    
                            ui_file.close()
                    
                    +        widget.show()
                    
                    
                    if __name__ == "__main__":
                        app = QApplication(sys.argv)
                        widget = MainWindow()
                    -    # widget.show()
                        sys.exit(app.exec())
                    

                    As beginner I spent a lot of time to figure out why. Can someone please make the generated code functional in Qt Creator when creating a new project?

                    JonBJ 1 Reply Last reply
                    0
                    • ErriezE Erriez

                      I had the same problem with PySide6 on Ubuntu 22.04 and Windows 10: Creating a new project with QMainWindow does not work. See my question on StackOverflow.

                      Complete fixed example:

                      # This Python file uses the following encoding: utf-8
                      import os
                      from pathlib import Path
                      import sys
                      
                      from PySide6.QtWidgets import QApplication, QMainWindow
                      from PySide6.QtCore import QFile
                      from PySide6.QtUiTools import QUiLoader
                      
                      
                      class MainWindow(QMainWindow):
                          def __init__(self, parent=None):
                              super().__init__(parent)
                              self.load_ui()
                      
                          def load_ui(self):
                              loader = QUiLoader()
                              path = Path(__file__).resolve().parent / "form.ui"
                              ui_file = QFile(path)
                              ui_file.open(QFile.ReadOnly)
                      
                      -        loader.load(ui_file, self)
                      +        widget = loader.load(ui_file, self)
                      
                              ui_file.close()
                      
                      +        widget.show()
                      
                      
                      if __name__ == "__main__":
                          app = QApplication(sys.argv)
                          widget = MainWindow()
                      -    # widget.show()
                          sys.exit(app.exec())
                      

                      As beginner I spent a lot of time to figure out why. Can someone please make the generated code functional in Qt Creator when creating a new project?

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

                      @Erriez said in Qt Creator and Pyside2 - Blank Window:

                      Can you please fix this issue when creating a new project?

                      Hi. What does this mean? If you mean can somebody here fix what Python code Designer generates you would have to report an issue to Qt developers, we are just a forum of users like yourself!

                      ErriezE 1 Reply Last reply
                      1
                      • JonBJ JonB

                        @Erriez said in Qt Creator and Pyside2 - Blank Window:

                        Can you please fix this issue when creating a new project?

                        Hi. What does this mean? If you mean can somebody here fix what Python code Designer generates you would have to report an issue to Qt developers, we are just a forum of users like yourself!

                        ErriezE Offline
                        ErriezE Offline
                        Erriez
                        wrote on last edited by Erriez
                        #16

                        @JonB Yes, my question is for the Qt developers to fix the generated code in Qt Creator. I had a bad start when creating a new project which does not work out of the box.
                        You're right, https://bugreports.qt.io is used to report bugs and not this forum. I'll follow-up here. Thanks!

                        1 Reply Last reply
                        1

                        • Login

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