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. Creating a QMainWindow app with PySide6 and Qt Designer
Forum Updated to NodeBB v4.3 + New Features

Creating a QMainWindow app with PySide6 and Qt Designer

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 1.7k 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.
  • P Offline
    P Offline
    Patchy4350
    wrote on last edited by Patchy4350
    #1

    I am attempting to create a Qt GUI with PySide6 and Qt Creator, and I need a menubar, so I have to use QMainWindow. My issue is that using the .ui file and the template doesn't actually work, it only produces a small window without anything else, no GUI elements or menubar. I am on MacOS Monterey.
    I've tried a couple of different approaches, but no luck. This is the auto-generated code, let's call it main.py.

    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):
            super(MainWindow, self).__init__()
            self.load_ui()
    
        def load_ui(self):
            loader = QUiLoader()
            path = os.fspath(Path(__file__).resolve().parent / "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 = MainWindow()
        widget.show()
        sys.exit(app.exec())
    

    And here's the form.ui file, where I've only added the menu.

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>Bux</class>
     <widget class="QMainWindow" name="Bux">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>450</width>
        <height>394</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>Bux</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <layout class="QVBoxLayout" name="verticalLayout"/>
      </widget>
      <widget class="QMenuBar" name="menubar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>450</width>
         <height>24</height>
        </rect>
       </property>
       <widget class="QMenu" name="menuFile">
        <property name="title">
         <string>File</string>
        </property>
       </widget>
       <widget class="QMenu" name="menuEdit">
        <property name="title">
         <string>Edit</string>
        </property>
       </widget>
       <widget class="QMenu" name="menuView">
        <property name="title">
         <string>View</string>
        </property>
       </widget>
       <addaction name="menuFile"/>
       <addaction name="menuEdit"/>
       <addaction name="menuView"/>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
     </widget>
     <resources/>
     <connections/>
    </ui>
    

    The reason for this seems to be that the .ui is not actually becoming the main application window; if I append show() to the UI loader (loader.load(ui_file, self).show()) and comment out widget.show(), I get my window up - but without a menubar as it is not in fact the main window.

    I've also tried converting the form.ui with pyside6-uic without luck. As this is an out of the box template, I'm quite confused, and after having spent several hours trying to find a solution I'm still at a loss... Does anyone have any insight or knows how to fix it?

    SGaistS 1 Reply Last reply
    0
    • P Patchy4350

      I am attempting to create a Qt GUI with PySide6 and Qt Creator, and I need a menubar, so I have to use QMainWindow. My issue is that using the .ui file and the template doesn't actually work, it only produces a small window without anything else, no GUI elements or menubar. I am on MacOS Monterey.
      I've tried a couple of different approaches, but no luck. This is the auto-generated code, let's call it main.py.

      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):
              super(MainWindow, self).__init__()
              self.load_ui()
      
          def load_ui(self):
              loader = QUiLoader()
              path = os.fspath(Path(__file__).resolve().parent / "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 = MainWindow()
          widget.show()
          sys.exit(app.exec())
      

      And here's the form.ui file, where I've only added the menu.

      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
       <class>Bux</class>
       <widget class="QMainWindow" name="Bux">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>450</width>
          <height>394</height>
         </rect>
        </property>
        <property name="windowTitle">
         <string>Bux</string>
        </property>
        <widget class="QWidget" name="centralwidget">
         <layout class="QVBoxLayout" name="verticalLayout"/>
        </widget>
        <widget class="QMenuBar" name="menubar">
         <property name="geometry">
          <rect>
           <x>0</x>
           <y>0</y>
           <width>450</width>
           <height>24</height>
          </rect>
         </property>
         <widget class="QMenu" name="menuFile">
          <property name="title">
           <string>File</string>
          </property>
         </widget>
         <widget class="QMenu" name="menuEdit">
          <property name="title">
           <string>Edit</string>
          </property>
         </widget>
         <widget class="QMenu" name="menuView">
          <property name="title">
           <string>View</string>
          </property>
         </widget>
         <addaction name="menuFile"/>
         <addaction name="menuEdit"/>
         <addaction name="menuView"/>
        </widget>
        <widget class="QStatusBar" name="statusbar"/>
       </widget>
       <resources/>
       <connections/>
      </ui>
      

      The reason for this seems to be that the .ui is not actually becoming the main application window; if I append show() to the UI loader (loader.load(ui_file, self).show()) and comment out widget.show(), I get my window up - but without a menubar as it is not in fact the main window.

      I've also tried converting the form.ui with pyside6-uic without luck. As this is an out of the box template, I'm quite confused, and after having spent several hours trying to find a solution I'm still at a loss... Does anyone have any insight or knows how to fix it?

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      All your menus are empty. You should add at least add one action to each.

      Using uic is the correct solution.

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

      P 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi and welcome to devnet,

        All your menus are empty. You should add at least add one action to each.

        Using uic is the correct solution.

        P Offline
        P Offline
        Patchy4350
        wrote on last edited by
        #3

        @SGaist Aaaah, perfect, thanks. Works now.

        1 Reply Last reply
        0
        • P Patchy4350 has marked this topic as solved on

        • Login

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