QTabWidget refusing to auto-resize
I'm a开发者_StackOverflow中文版ttempting 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.
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!
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>
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) ;
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?
精彩评论