【问题标题】:Qt - trying to align two GroupBoxes in a layoutQt - 尝试在布局中对齐两个 GroupBox
【发布时间】:2021-08-31 09:02:15
【问题描述】:

我的表单的一部分包含 QGroupBox (Status Box) 和 4 个子 QGroupBoxes 以网格布局 (2x2) 排列。底部的两个QGroupBoxes(Widget 1 BoxWidget 2 Box)包含固定大小的小部件(设置minimumSizemaximumSize),因此它们在两个方向上都无法调整大小。由于严格的尺寸限制,QGroupBoxes(Summary BoxHelper Box)的顶行只能在垂直方向上调整大小。

麻烦的部分来了。左上角QGroupBox (Summary Box) 具有 5x3 网格布局,而右上角 (Helper Box) 具有 6 行的垂直布局。如果我有如图 1 所示的天真的小部件放置,Qt 正在扩大顶行中两个标签的垂直尺寸,以使两个 QGroupBoxes 的高度相等(参见图 1 上的红色箭头)。

这绝对是我不想要的,所以我在Summary Box 的底部添加了垂直间隔,乍一看它有效(图 2)。但仅从第一眼看...你看到的是我整个表格的最小高度和垫片的底部和Helper Box中的最后一个QCheckBox似乎是对齐的。

如果我垂直扩展我的表格,这个间隔会增长一点,这会导致顶部QGroupBoxes 的高度增加。结果QCheckBoxes 之间的间距也增加了,我们还可以看到右上角的框的顶部和底部间距不相等(参见图 3 中的红色箭头)。

我尝试使用sizeType 作为我的垂直垫片。如果我将其设置为MinimumMinimumExpanding,则间隔不会在调整大小时增长(也不会缩小),但它似乎会扩展到图 3 上的大小(破坏QCheckBox 之间的间距也是)。如果我将其设置为 MaximumPreferredExpanding,那么我观察到的行为与上述图 3 相同。

在不影响元素间距的情况下,在一行网格中实现两个QGroupBoxes 对齐的正确方法是什么(例如,在这种情况下,使垂直间隔器仅适合单行网格布局并且从不扩展/收缩)?

【问题讨论】:

    标签: c++ qt layout


    【解决方案1】:

    如果两个 QGroupBoxes 具有相同数量的子元素,并且每行至少有一个具有 Expanding 垂直策略的子元素,则项目将对齐。而不是间隔使用QWidget

    出于演示目的,我删除了不相关的小部件并将行数减少到 4(更少的 xml)。

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>Form</class>
     <widget class="QWidget" name="Form">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>467</width>
        <height>314</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>Form</string>
      </property>
      <layout class="QHBoxLayout" name="horizontalLayout">
       <item>
        <widget class="QGroupBox" name="groupBox">
         <property name="title">
          <string>GroupBox</string>
         </property>
         <layout class="QGridLayout" name="gridLayout">
          <item row="0" column="0">
           <widget class="QLabel" name="label">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="Expanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>TextLabel</string>
            </property>
           </widget>
          </item>
          <item row="3" column="2">
           <widget class="QWidget" name="dummy" native="true">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="Expanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
           </widget>
          </item>
          <item row="1" column="0" colspan="2">
           <widget class="QLabel" name="label_4">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="Expanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>TextLabel</string>
            </property>
           </widget>
          </item>
          <item row="1" column="2">
           <widget class="QLineEdit" name="lineEdit"/>
          </item>
          <item row="2" column="2">
           <widget class="QLineEdit" name="lineEdit_4">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
           </widget>
          </item>
          <item row="2" column="0">
           <widget class="QLabel" name="label_5">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Preferred" vsizetype="Expanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>TextLabel</string>
            </property>
           </widget>
          </item>
         </layout>
        </widget>
       </item>
       <item>
        <widget class="QGroupBox" name="groupBox_2">
         <property name="title">
          <string>GroupBox</string>
         </property>
         <layout class="QVBoxLayout" name="verticalLayout">
          <item>
           <widget class="QCheckBox" name="checkBox">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Minimum" vsizetype="Expanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>CheckBox</string>
            </property>
           </widget>
          </item>
          <item>
           <widget class="QCheckBox" name="checkBox_4">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Minimum" vsizetype="Expanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>CheckBox</string>
            </property>
           </widget>
          </item>
          <item>
           <widget class="QCheckBox" name="checkBox_5">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Minimum" vsizetype="Expanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>CheckBox</string>
            </property>
           </widget>
          </item>
          <item>
           <widget class="QCheckBox" name="checkBox_6">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Minimum" vsizetype="Expanding">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>CheckBox</string>
            </property>
           </widget>
          </item>
         </layout>
        </widget>
       </item>
      </layout>
     </widget>
     <resources/>
     <connections/>
    </ui>
    

    【讨论】:

    • 哇,谢谢,用 QWidget 替换 spacer 效果很好!我还发现不需要为所有行设置扩展策略 - 它只适用于添加虚拟 QWidget
    猜你喜欢
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 2014-11-10
    • 2011-04-22
    • 2021-09-06
    • 2021-09-17
    相关资源
    最近更新 更多