Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
widgets are not showing when inheriting the QMainWindow class
-
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_())
-
@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())
-
@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()
returnsNone
since it is only set after loading a UI if I'm not mistaken
-
This post is deleted!