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. widgets are not showing when inheriting the QMainWindow class

widgets are not showing when inheriting the QMainWindow class

Scheduled Pinned Locked Moved Unsolved Qt for Python
7 Posts 5 Posters 2.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.
  • R Offline
    R Offline
    rhx9
    wrote on last edited by
    #1

    Hi,
    I created a test project in qt-creator and choosing "qt for python- window (UI) file" as a template and a QMainWindow as a base class, then added a single push button and applied a grid layout to the centralwidget but after clicking Ctrl+r to run the project I only see a blank window with no widgets.

    form.ui (generated by qt creator, not modified):

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>test</class>
     <widget class="QMainWindow" name="test">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>800</width>
        <height>600</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>test</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <layout class="QGridLayout" name="gridLayout">
        <item row="0" column="0">
         <widget class="QPushButton" name="pushButton">
          <property name="text">
           <string>PushButton</string>
          </property>
         </widget>
        </item>
       </layout>
      </widget>
      <widget class="QMenuBar" name="menubar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>800</width>
         <height>34</height>
        </rect>
       </property>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    

    main.py (generated by qt creator, not modified):

    # This Python file uses the following encoding: utf-8
    import sys
    import os
    
    
    from PySide2.QtWidgets import QApplication, QMainWindow
    from PySide2.QtCore import QFile
    from PySide2.QtUiTools import QUiLoader
    
    
    class test(QMainWindow):
        def __init__(self):
            super(test, self).__init__()
            self.load_ui()
    
        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 = test()
        widget.show()
        sys.exit(app.exec_())
    
    

    note that choosing Qwidget as a base class works fine and I can see the push button without a problem, if I convert the UI file using the pyuic command it works and runs just fine, also if I load the UI file without inheriting a class it works fine as well like so:

    import sys
    
    
    from PySide2.QtWidgets import QApplication
    from PySide2.QtUiTools import QUiLoader
    
    
    if __name__ == "__main__":
        app = QApplication([])
        w = QUiLoader().load("form.ui")
        w.show()
        sys.exit(app.exec_())
    
    jsulmJ 1 Reply Last reply
    0
    • R rhx9

      Hi,
      I created a test project in qt-creator and choosing "qt for python- window (UI) file" as a template and a QMainWindow as a base class, then added a single push button and applied a grid layout to the centralwidget but after clicking Ctrl+r to run the project I only see a blank window with no widgets.

      form.ui (generated by qt creator, not modified):

      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
       <class>test</class>
       <widget class="QMainWindow" name="test">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>800</width>
          <height>600</height>
         </rect>
        </property>
        <property name="windowTitle">
         <string>test</string>
        </property>
        <widget class="QWidget" name="centralwidget">
         <layout class="QGridLayout" name="gridLayout">
          <item row="0" column="0">
           <widget class="QPushButton" name="pushButton">
            <property name="text">
             <string>PushButton</string>
            </property>
           </widget>
          </item>
         </layout>
        </widget>
        <widget class="QMenuBar" name="menubar">
         <property name="geometry">
          <rect>
           <x>0</x>
           <y>0</y>
           <width>800</width>
           <height>34</height>
          </rect>
         </property>
        </widget>
        <widget class="QStatusBar" name="statusbar"/>
       </widget>
       <resources/>
       <connections/>
      </ui>
      
      

      main.py (generated by qt creator, not modified):

      # This Python file uses the following encoding: utf-8
      import sys
      import os
      
      
      from PySide2.QtWidgets import QApplication, QMainWindow
      from PySide2.QtCore import QFile
      from PySide2.QtUiTools import QUiLoader
      
      
      class test(QMainWindow):
          def __init__(self):
              super(test, self).__init__()
              self.load_ui()
      
          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 = test()
          widget.show()
          sys.exit(app.exec_())
      
      

      note that choosing Qwidget as a base class works fine and I can see the push button without a problem, if I convert the UI file using the pyuic command it works and runs just fine, also if I load the UI file without inheriting a class it works fine as well like so:

      import sys
      
      
      from PySide2.QtWidgets import QApplication
      from PySide2.QtUiTools import QUiLoader
      
      
      if __name__ == "__main__":
          app = QApplication([])
          w = QUiLoader().load("form.ui")
          w.show()
          sys.exit(app.exec_())
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @rhx9 said in widgets are not showing when inheriting the QMainWindow class:

      loader.load(ui_file, self)

      Use https://doc.qt.io/qt-5/qmainwindow.html#centralWidget as parent:

      loader.load(ui_file, self.centralWidget())
      

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

      R 1 Reply Last reply
      1
      • jsulmJ jsulm

        @rhx9 said in widgets are not showing when inheriting the QMainWindow class:

        loader.load(ui_file, self)

        Use https://doc.qt.io/qt-5/qmainwindow.html#centralWidget as parent:

        loader.load(ui_file, self.centralWidget())
        
        R Offline
        R Offline
        rhx9
        wrote on last edited by
        #3

        @jsulm said in widgets are not showing when inheriting the QMainWindow class:

        @rhx9 said in widgets are not showing when inheriting the QMainWindow class:

        loader.load(ui_file, self)

        Use https://doc.qt.io/qt-5/qmainwindow.html#centralWidget as parent:

        loader.load(ui_file, self.centralWidget())
        

        Nope, doesn't work. also printing self.centralWidget() returns None since it is only set after loading a UI if I'm not mistaken

        JonBJ K 2 Replies Last reply
        0
        • R rhx9

          @jsulm said in widgets are not showing when inheriting the QMainWindow class:

          @rhx9 said in widgets are not showing when inheriting the QMainWindow class:

          loader.load(ui_file, self)

          Use https://doc.qt.io/qt-5/qmainwindow.html#centralWidget as parent:

          loader.load(ui_file, self.centralWidget())
          

          Nope, doesn't work. also printing self.centralWidget() returns None since it is only set after loading a UI if I'm not mistaken

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • R rhx9

            @jsulm said in widgets are not showing when inheriting the QMainWindow class:

            @rhx9 said in widgets are not showing when inheriting the QMainWindow class:

            loader.load(ui_file, self)

            Use https://doc.qt.io/qt-5/qmainwindow.html#centralWidget as parent:

            loader.load(ui_file, self.centralWidget())
            

            Nope, doesn't work. also printing self.centralWidget() returns None since it is only set after loading a UI if I'm not mistaken

            K Offline
            K Offline
            KNewman
            wrote on last edited by
            #5

            @rhx9 I had the same issue, solution:

            change

            widget.show()

            to

            widget.centralWidget().show()

            Loading the ui file populates the centralWidget property of the QMainWindow object, which is the primary content of the main window.

            There is no show() method of QMainWindow, the parent class of test.

            See https://doc.qt.io/qt-5/qmainwindow.html

            jeremy_kJ 1 Reply Last reply
            0
            • jeremy_kJ Offline
              jeremy_kJ Offline
              jeremy_k
              wrote on last edited by
              #6

              Aside from the visual issue, the ui file is creating a QMainWindow that is then populated into another QMainWindow.

              Asking a question about code? http://eel.is/iso-c++/testcase/

              1 Reply Last reply
              1
              • K KNewman

                @rhx9 I had the same issue, solution:

                change

                widget.show()

                to

                widget.centralWidget().show()

                Loading the ui file populates the centralWidget property of the QMainWindow object, which is the primary content of the main window.

                There is no show() method of QMainWindow, the parent class of test.

                See https://doc.qt.io/qt-5/qmainwindow.html

                jeremy_kJ Offline
                jeremy_kJ Offline
                jeremy_k
                wrote on last edited by
                #7

                @KNewman said in widgets are not showing when inheriting the QMainWindow class:

                @rhx9 I had the same issue, solution:

                There is no show() method of QMainWindow, the parent class of test.

                QMainWindow derives from QWidget, which provides show().

                See https://doc.qt.io/qt-5/qmainwindow.html

                It's under the "List of all members, including inherited members" link.

                Asking a question about code? http://eel.is/iso-c++/testcase/

                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