Creating a QMainWindow app with PySide6 and Qt Designer
-
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 itmain.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 appendshow()
to the UI loader (loader.load(ui_file, self).show()
) and comment outwidget.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
withpyside6-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? -
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 itmain.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 appendshow()
to the UI loader (loader.load(ui_file, self).show()
) and comment outwidget.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
withpyside6-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?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.
-
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.
@SGaist Aaaah, perfect, thanks. Works now.
-