Solved Qt Creator and Pyside2 - Blank Window
-
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
-
Actually, for QMainWindow, I don't think PySide2 allows the UI file to be loaded from within the constructor.
-
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
-
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.
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
-
@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.
-
@Marcus-Adamski
Now going all the way back to your original Designer UI files, and why your code works with aQWidget
but not with aQMainWindow
.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 aQWidget
a direct child on another (non-container)QWidget
such asQMainWindow.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-windowbut 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 callshow()
on, and got rid of yourshow()
on the object constructed viamain_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. -
-
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?
-
@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!
-
@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!