QTabWidget 拒绝自动调整大小

发布于 2024-11-13 06:32:12 字数 312 浏览 5 评论 0原文

我试图在 qtcreator 中采用以下布局:

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

每个具有 sizePolicy 的控件都设置为 Expanding。尽管如此,选项卡小部件和我放在该页面小部件上的其他任何东西都不会自动调整大小。

我是 Qt 新手,所以如果这是一个明显的修复,请原谅我。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

诠释孤独 2024-11-20 06:32:13

这是重复的吗
如何制作 Qt 小部件随着窗口大小而增长? ?

我认为这是重复的,因为如果你的小部件树中的每个项目都有一个布局,我对 Qt 4.7 的测试给出了以下输出,右侧是我的对象检查器。
我只是放入了小部件并对其进行了布局,我没有更改任何大小来扩展,因为这绝对是不必要的!

带有 exe 和创建者屏幕截图的图像

左侧是正在运行的 exe,右侧是创建者屏幕截图。
这是我的用户界面:

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

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>
九公里浅绿 2024-11-20 06:32:13

您需要以下层次结构(请注意QLayout):

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

因此代码如下所示:

QLayout* MyLayout = new QVBoxLayout (MyPageWidget) ;
MyLayout -> setSpacing (0) ;
MyLayout -> setContentsMargins (0, 0, 0, 0) ;
MyLayout -> addWidget (MyTabWidget) ;

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) ;
看海 2024-11-20 06:32:13

我在设计器中创建了一个新的顶级窗口(QWidget),并将两个堆叠的小部件放在上面,按住 Ctrl 键单击它们以选择它们,然后右键单击它们并选择“在分割器中水平布局”。然后我右键单击顶层 QWidget 并选择“布局在网格中”。此后,两个堆叠的小部件将扩展以填充可用空间,并且它们会随着顶层窗口的大小而适当地增长和收缩。也许您只需要为顶级小部件设置布局?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文