3

I'm attempting to have the following layout in qtcreator:

QWidget
  QSplitter
    QStackedWidget
      ...
    QStackedWidget
      QWidget (page)
        QTabWidget

Every single control that has a sizePolicy is set to Expanding. Despite this, neither the tab widget nor anything else I drop on that page widget gets autosized.

I'm new to Qt, so forgive me if this is an obvious fix. Thanks.

Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
Reinderien
  • 11,755
  • 5
  • 49
  • 77

3 Answers3

3

Is this a duplicate of How to make a Qt Widget grow with the window size? ?

I think it is a duplicate, cause if every item in your tree of widgets has a layout, my test on Qt 4.7 gave the following output, on the right side is my object inspector. I did just put in the widgets and layouted them, i did not change any size things to expanding, as this is absolutely unnecessary!

Image with exe and creator screenshot

On the left is the running exe, on the right is the Creator screenshot. And here is my UI:

    <?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>545</width>
    <height>441</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QHBoxLayout" name="horizontalLayout">
    <item>
     <widget class="QSplitter" name="splitter">
      <property name="orientation">
       <enum>Qt::Vertical</enum>
      </property>
      <widget class="QStackedWidget" name="stackedWidget">
       <property name="currentIndex">
        <number>0</number>
       </property>
       <widget class="QWidget" name="page_1_1">
        <property name="layoutDirection">
         <enum>Qt::LeftToRight</enum>
        </property>
        <layout class="QGridLayout" name="gridLayout_2">
         <item row="0" column="0">
          <widget class="QTabWidget" name="tabWidget_1">
           <property name="currentIndex">
            <number>0</number>
           </property>
           <widget class="QWidget" name="tab_3">
            <attribute name="title">
             <string>Tab 1</string>
            </attribute>
            <layout class="QGridLayout" name="gridLayout_4">
             <item row="0" column="0">
              <widget class="QDateTimeEdit" name="dateTimeEdit_2"/>
             </item>
             <item row="1" column="0">
              <widget class="QDateTimeEdit" name="dateTimeEdit"/>
             </item>
            </layout>
           </widget>
           <widget class="QWidget" name="tab_4">
            <attribute name="title">
             <string>Tab 2</string>
            </attribute>
            <layout class="QGridLayout" name="gridLayout_5">
             <item row="0" column="0">
              <widget class="QCheckBox" name="checkBox_2">
               <property name="text">
                <string>CheckBox</string>
               </property>
              </widget>
             </item>
             <item row="1" column="0">
              <widget class="QCheckBox" name="checkBox">
               <property name="text">
                <string>CheckBox</string>
               </property>
              </widget>
             </item>
            </layout>
           </widget>
          </widget>
         </item>
        </layout>
       </widget>
      </widget>
      <widget class="QStackedWidget" name="stackedWidget_2">
       <widget class="QWidget" name="page_2_1">
        <layout class="QGridLayout" name="gridLayout_3">
         <item row="0" column="0">
          <widget class="QTabWidget" name="tabWidget_2">
           <widget class="QWidget" name="tab">
            <attribute name="title">
             <string>Tab 1</string>
            </attribute>
            <layout class="QGridLayout" name="gridLayout">
             <item row="0" column="0">
              <widget class="QDateEdit" name="dateEdit"/>
             </item>
             <item row="1" column="0">
              <widget class="QDateEdit" name="dateEdit_2"/>
             </item>
            </layout>
           </widget>
           <widget class="QWidget" name="tab_2">
            <attribute name="title">
             <string>Tab 2</string>
            </attribute>
            <layout class="QGridLayout" name="gridLayout_6">
             <item row="0" column="0">
              <widget class="QCheckBox" name="checkBox_4">
               <property name="text">
                <string>CheckBox</string>
               </property>
              </widget>
             </item>
             <item row="1" column="0">
              <widget class="QCheckBox" name="checkBox_3">
               <property name="text">
                <string>CheckBox</string>
               </property>
              </widget>
             </item>
            </layout>
           </widget>
          </widget>
         </item>
        </layout>
       </widget>
      </widget>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>545</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>
Community
  • 1
  • 1
Jens
  • 6,173
  • 2
  • 24
  • 43
  • It seems suspicious that your page widget icons look different than mine. Yours appear to have the grid layout icon, whereas mine appear to have a red (/) symbol on them. Does this mean that my pages have no layout? How would I apply a layout to my pages? – Reinderien Jun 03 '11 at 20:08
  • That was the problem after all. I finally figured out how to apply a layout to the page widgets. – Reinderien Jun 03 '11 at 20:14
  • Just to answer the comment before the last one: the red crossed icon indeed means: NO LAYOUT! And if in the hierarchy of widgets one has no layout, automatic resizing is broken! – Jens Jun 04 '11 at 13:16
2

You need the following hierarchy note the QLayout):

QWidget
  QSplitter
    QStackedWidget
      ...
    QStackedWidget
      QWidget (page)
        QLayout
          QTabWidget

So the code is something like this:

QLayout* MyLayout = new QVBoxLayout (MyPageWidget) ;
MyLayout -> setSpacing (0) ;
MyLayout -> setContentsMargins (0, 0, 0, 0) ;
MyLayout -> addWidget (MyTabWidget) ;
TonyK
  • 16,761
  • 4
  • 37
  • 72
  • Even if I add a layout between the tab widget and the page widget, it doesn't work. The tab widget resizes to its layout, but the layout doesn't resize to the page. – Reinderien Jun 03 '11 at 14:17
  • @kidneyofnothing: That's hard to believe! How do you know? – TonyK Jun 03 '11 at 16:36
1

I created a new top level window (QWidget) in designer and drop two stacked widgets on it, ctrl-click both of them to select them and then right-clicked them and selected "Layout Horizontally in Splitter". I then right clicked on the top level QWidget and selected "Layout in a Grid". After this, the two stacked widgets expand to fill the avaialable space and they properly grow and shrink with the top level window. Perhaps you just need to set a layout for the top level widget?

Arnold Spence
  • 21,942
  • 7
  • 74
  • 67
  • The two stacked widgets had already been expanding properly. It's the control on the inside of the stacked widget (inside of the stacked page) that doesn't resize. – Reinderien Jun 03 '11 at 12:59