Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with adding QStackWidget or QTabWidget as child
QtWS25 Last Chance

Problem with adding QStackWidget or QTabWidget as child

Scheduled Pinned Locked Moved Unsolved General and Desktop
qstackedwidgetsqtdesigner
11 Posts 3 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pythonic person
    wrote on 19 Jan 2022, 13:34 last edited by
    #1

    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 buttons

    The 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?

    J 1 Reply Last reply 19 Jan 2022, 13:38
    0
    • P Pythonic person
      19 Jan 2022, 13:34

      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 buttons

      The 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?

      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 19 Jan 2022, 13:38 last edited by
      #2

      @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?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Pythonic person
        wrote on 19 Jan 2022, 13:44 last edited by
        #3

        As you can see here the QStackWidget takes all the views.
        Screenshot (147).png

        P 1 Reply Last reply 19 Jan 2022, 13:49
        0
        • P Pythonic person
          19 Jan 2022, 13:44

          As you can see here the QStackWidget takes all the views.
          Screenshot (147).png

          P Offline
          P Offline
          Pl45m4
          wrote on 19 Jan 2022, 13:49 last edited by Pl45m4
          #4

          @Pythonic-person

          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 your sizePolicy 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.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Pythonic person
            wrote on 19 Jan 2022, 13:56 last edited by Pythonic person
            #5

            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"))
            
            
            P 1 Reply Last reply 19 Jan 2022, 13:59
            0
            • P Pythonic person
              19 Jan 2022, 13:56

              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"))
              
              
              P Offline
              P Offline
              Pl45m4
              wrote on 19 Jan 2022, 13:59 last edited by Pl45m4
              #6

              @Pythonic-person

              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, a QPushButton... You can also tweak the layout stretch of your main layout to ensure that Widget X is as twice as big as Widget Y


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              1 Reply Last reply
              0
              • P Offline
                P Offline
                Pythonic person
                wrote on 19 Jan 2022, 14:07 last edited by
                #7

                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)
                Untitled.png

                P 1 Reply Last reply 19 Jan 2022, 14:16
                0
                • P Pythonic person
                  19 Jan 2022, 14:07

                  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)
                  Untitled.png

                  P Offline
                  P Offline
                  Pl45m4
                  wrote on 19 Jan 2022, 14:16 last edited by Pl45m4
                  #8

                  @Pythonic-person

                  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 your page widget layout and not next to the stackedWidget?!

                  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 of your QStackedWidget covers everything.


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  P 1 Reply Last reply 19 Jan 2022, 14:23
                  0
                  • P Pl45m4
                    19 Jan 2022, 14:16

                    @Pythonic-person

                    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 your page widget layout and not next to the stackedWidget?!

                    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 of your QStackedWidget covers everything.

                    P Offline
                    P Offline
                    Pythonic person
                    wrote on 19 Jan 2022, 14:23 last edited by Pythonic person
                    #9

                    @Pl45m4

                    @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 your page 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

                    P 1 Reply Last reply 19 Jan 2022, 14:34
                    0
                    • P Pythonic person
                      19 Jan 2022, 14:23

                      @Pl45m4

                      @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 your page 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

                      P Offline
                      P Offline
                      Pl45m4
                      wrote on 19 Jan 2022, 14:34 last edited by Pl45m4
                      #10

                      @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):

                      StackedWidget.png

                      StackedTree.png

                      (I also haven't changed anything, just placed the widgets)


                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      1 Reply Last reply
                      1
                      • P Offline
                        P Offline
                        Pythonic person
                        wrote on 19 Jan 2022, 15:56 last edited by
                        #11

                        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

                        1 Reply Last reply
                        0

                        2/11

                        19 Jan 2022, 13:38

                        9 unread
                        • Login

                        • Login or register to search.
                        2 out of 11
                        • First post
                          2/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved