Problem with adding QStackWidget or QTabWidget as child
-
Am using Qt Desginer, i added the following
MainWindow ->
---->centralWidget (verticalLayout)--------> main (QFrame) (horizatlLayout)
------ -----> QStackedWidget
-----------------> page 1
-----------------> page 2---------> top_bar (QFrame) (horizantlLayout)
----------------> other frames here with layout for both logo and top bar buttonsThe problem is: after I added the QStackedWidget as a child of the main widget => that stack widget takes the size of the whole screen like MainWindow, It should take the size of the main widget. and then nothing is visible but that QstackWidget because it covers all of the screen views.
Am using QtDesginer and I didn't use any pyqt5 or PySide code yet, am working only with the UI.
Any ideas why is that happened?
-
@Pythonic-person said in Problem with adding QStackWidget or QTabWidget as child:
that stack widget takes the size of the whole screen like MainWindow
Then you did not add it to main, but to centralWidget.
Can you post the screen-shot of the designer? -
As you can see here the QStackWidget takes all the views.
-
What if you add other stuff?! The stackedWidget should share the space with other widgets then... (when there are other widgets in
main
layout)
If it's the only widgets for now, why should it be small? Depending on yoursizePolicy
and hints, the widget takes the space it can get from its parent layout (until it reaches min/max width or height).See the example image here: https://doc.qt.io/qt-5/qhboxlayout.html#details
When there are 5 buttons, they share the space, if there would be only one button, it would occupy the whole "row" / all the available space. -
Yes, there are other widgets in the main layout, and they are empty as you said they are small, they became small after I added the stack widgets. And if I delete it (stack widgets) they (other widgets) will return to the normal size.
Actually, I didn't change anything with sizePolicy, I will try to show you the code..
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'C:/Users/user/AppData/Local/Temp/new_design v0-3 FCBcDk.ui' # # Created by: PyQt5 UI code generator 5.15.4 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. from PyQt5 import QtCore, QtGui, QtWidgets class Ui_main_window(object): def setupUi(self, main_window): main_window.setObjectName("main_window") main_window.resize(1225, 912) self.centralwidget = QtWidgets.QWidget(main_window) self.centralwidget.setObjectName("centralwidget") self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget) self.verticalLayout.setContentsMargins(0, 0, 0, 0) self.verticalLayout.setSpacing(0) self.verticalLayout.setObjectName("verticalLayout") self.top_bar = QtWidgets.QFrame(self.centralwidget) self.top_bar.setMaximumSize(QtCore.QSize(16777215, 45)) self.top_bar.setStyleSheet("background-color: rgb(0, 170, 0);") self.top_bar.setObjectName("top_bar") self.horizontalLayout = QtWidgets.QHBoxLayout(self.top_bar) self.horizontalLayout.setContentsMargins(0, 0, 0, 0) self.horizontalLayout.setSpacing(0) self.horizontalLayout.setObjectName("horizontalLayout") self.logo_container = QtWidgets.QFrame(self.top_bar) self.logo_container.setStyleSheet("background-color: rgb(0, 255, 127);") self.logo_container.setObjectName("logo_container") self.horizontalLayout.addWidget(self.logo_container) self.top_bar_buttons_container = QtWidgets.QFrame(self.top_bar) self.top_bar_buttons_container.setStyleSheet("background-color: rgb(170, 255, 127);") self.top_bar_buttons_container.setObjectName("top_bar_buttons_container") self.horizontalLayout.addWidget(self.top_bar_buttons_container) self.verticalLayout.addWidget(self.top_bar) self.main = QtWidgets.QFrame(self.centralwidget) self.main.setStyleSheet("background-color: rgb(0, 0, 0);") self.main.setObjectName("main") self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.main) self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.widget = QtWidgets.QWidget(self.main) self.widget.setObjectName("widget") self.horizontalLayout_2.addWidget(self.widget) self.widget_2 = QtWidgets.QWidget(self.main) self.widget_2.setObjectName("widget_2") self.horizontalLayout_2.addWidget(self.widget_2) self.stackedWidget = QtWidgets.QStackedWidget(self.main) self.stackedWidget.setObjectName("stackedWidget") self.page_3 = QtWidgets.QWidget() self.page_3.setObjectName("page_3") self.stackedWidget.addWidget(self.page_3) self.page_4 = QtWidgets.QWidget() self.page_4.setObjectName("page_4") self.stackedWidget.addWidget(self.page_4) self.horizontalLayout_2.addWidget(self.stackedWidget) self.verticalLayout.addWidget(self.main) main_window.setCentralWidget(self.centralwidget) self.retranslateUi(main_window) QtCore.QMetaObject.connectSlotsByName(main_window) def retranslateUi(self, main_window): _translate = QtCore.QCoreApplication.translate main_window.setWindowTitle(_translate("main_window", "MainWindow"))
-
Then tell us what it should look like / what do you expect...
Edit:
I will try to show you the code..
The code you've posted is the auto-generated code from your
ui
file... basically your "design" as C-class, or in your case, as Python class...Edit^2:
Check your sizePolicies...
QStackedWidget
has other defaults as, for example, aQPushButton
... You can also tweak the layout stretch of yourmain
layout to ensure thatWidget X
is as twice as big asWidget Y
-
the red border means here I will add the QStackWidgits because the top_menu should always be shown (it contains a logo and minimize maximize close buttons)
-
This cant work because if you flip the pages, the whole area will switch... for example, you cant put buttons to switch the pages on one page... you would need them on every
page
widget. And then they belong in yourpage
widget layout and not next to the stackedWidget?!For the approach above, you need to design the
page
widgets and not the layout where yourQStackedWidget
is in...
Or am I mistaken?! Themain
QFrame still covers the wholemainWindow
(->centralWidget
) and because there are no pages at the moment, the blank first page of yourQStackedWidget
covers everything. -
@Pl45m4 said in Problem with adding QStackWidget or QTabWidget as child:
This cant work because if you flip the pages, the whole area will switch... for example, you cant put buttons to switch the pages on one page... you would need them on every
page
widget. And then they belong in yourpage
widget layout and not next to the stackedWidget?!Yes, You are right, I was planning to put these two buttons on the two pages as you said. Do you think there will be a problem with that?
For the approach above, you need to design the page widgets and not the layout where your QStackedWidget is in...
Or am I mistaken?! The main QFrame still covers the whole mainWindow (-> centralWidget) and because there are no pages at the moment, the blank first page covers everything.That's right, but my problem is not in the pages at the moment, As you can see from the structure in the first post, I have both the Main layout and the top_bar layout. The problem is that after I added the stackwidget to the main layout it covered all the window even covering the top_bar which shouldn't do that.
Did I explain it well?
Thank you for helping me.--Edit--
centralwidget has two wegits inside : main and top_bar
main contained :the stackwidgets
top_bar contains: two frames logo and bottons -
@Pythonic-person said in Problem with adding QStackWidget or QTabWidget as child:
I was planning to put these two buttons on the two pages as you said. Do you think there will be a problem with that?
No, both ways are possible, but your then either your description is wrong or your image (or at least confusing)...
One possibility (with buttons to switch outside the stackedWidget):
(I also haven't changed anything, just placed the widgets)
-
Thank you it's worked, it seems that my other Frames didn't contain anything so that's why the StackWidgets covered all the window..
Thank you @Pl45m4