PySide6 - how to properly display central widget
-
Tools and SW:
- python: 3.10.5
- QTCreator 9.0.0 Community
- PySide 6.4.1
I have this setup in QTCreator:
My code starts with:
class MainApp(): def __init__(self, qapp = None, qargs = None): self.ui = MainWindow(self) self.ui.show() if __name__ == '__main__': faulthandler.enable() app = QApplication(sys.argv) mainapp = MainApp(qapp = app, qargs = sys.argv) sys.exit(app.exec())
I have a custom MainWindow class, that extends QMainWindow.
# Main window class class MainWindow(QMainWindow): def __init__(self, app): super().__init__() self.app = app self.load_ui() # This is default code from Qt to load .UI file self.load_widgets() # Loads widgets to memory # This is where the funny code comes # Loads widgets from UI to fw def load_widgets(self): self.w_top_widget = self.findChildren(QMainWindow, 'MainWindow')[0] self.w_top_central_widget = self.findChildren(QWidget, 'centralwidget')[0] # Other widgets...
In the above example, you can find the
# This is where the funny code comes
where I am facing the issue.
Running the code as is, I only see empty screen.
If I add the code (to the placeholder part), then I get 2 screens
# This is where the funny code comes self.centralWidget().show()
If I add code below, I again receive empty screen
# This is where the funny code comes self.setCentralWidget(self.w_top_widget)
If I set central widget the one that is part of top-top widget, I get screen, but no menu and status bar.
# This is where the funny code comes #self.setCentralWidget(self.w_top_widget) self.setCentralWidget(self.w_top_central_widget)
No menu:
How should I properly set central widget to show one window only, with top menu and statusbar, and with windows toolbar?
One more thing. If I do not call self.ui.show() after MainWindow, then window properly pops up, but there is no windows taskbar icon.
The code
class MainApp(): def __init__(self, qapp = None, qargs = None): self.ui = MainWindow(self) # Not calling this, project window will properly show, but no windows taskbar (Windows 11) #self.ui.show() class MainWindow(QMainWindow): def __init__(self, app): super().__init__() self.app = app self.load_ui() self.load_widgets() self.setWindowTitle('MainWindow') self.resize(1280, 800) # This is where the funny code comes self.setCentralWidget(self.w_top_widget) self.w_top_widget.show() def load_widgets(self): self.w_top_widget = self.findChildren(QMainWindow, 'MainWindow')[0] self.w_top_central_widget = self.findChildren(QWidget, 'centralwidget')[0]
produces following (no taskbar icon)
-
Hi,
What is the point of the MainApp class ?
Why is w_top_widget a QMainWindow ?
Can you provide a schema of the design you want to achieve ? -
Next to the GUI, project includes some other functionality too. I'm in the Qt learning process, I simply thought it might be better to separate true UI activity from the rest of lower-level parts.
MainApp: Class that initializes specific DLL for low-level communication with serial port and starts UI
MainWindow: Is an extension of QMainWindow, a top-view class that was set by QtCreator when I started new project.w_top_widget is a very top widget in the UI.
Not sure I fully understand the "schema of the design". You mean UI file or application design structure?
Or better to ask - what is the correct way of doing it?
-
I started a new project in QtCreator 9.0.0 Community, with dynamic UI load:
Adding button and vertical orientation of the central widget, default generated Qt code won't show anything on screen.
I made a video, that first creates new project (unfortunately Win screen capture did not shot wizard). A window only shows when I add more code. And in some cases I receive 2 windows, in some cases only one (but then no windows taskbar icon..).
-
Sorry, I lost track of this thread.
Even though you generated the project, can you post the files content here so we can be sure people are testing the same code you have ?