Qt - 试图在布局中对齐两个 GroupBox
Qt - trying to align two GroupBoxes in a layout
我的表单的一部分包含 QGroupBox
(Status Box
),其中 4 个子 QGroupBox
es 以网格布局 (2x2) 排列。底部的两个 QGroupBox
es(Widget 1 Box
和 Widget 2 Box
)包含固定大小的小部件(设置 minimumSize
和 maximumSize
),因此它们根本无法调整大小在两个方向上。由于严格的大小限制,QGroupBox
es(Summary Box
和 Helper Box
)的顶行只能在垂直方向调整大小。
麻烦的部分来了。左上角 QGroupBox
(Summary Box
) 具有 5x3 网格布局,而右上角 (Helper Box
) 具有 6 行的垂直布局。如果我有如图 1 所示的天真的小部件放置,Qt 会放大顶行中两个标签的垂直尺寸以使两个 QGroupBox
es 的高度相等(参见图 1 上的红色箭头)。
这绝对是我不想要的,所以我在 Summary Box
的底部添加了垂直垫片,乍一看它起作用了(图 2)。但仅从第一眼看...您看到的是我整个表格的最小高度和垫片的底部以及 Helper Box
中的最后一个 QCheckBox
似乎对齐。
如果我垂直扩展表格,这个垫片会变大一点,这会导致两个顶部 QGroupBox
的高度增加。结果 QCheckBox
es 之间的间距也增加了,我们还可以看到右上角框的顶部和底部间距不相等(见图 3 中的红色箭头)。
我尝试使用 sizeType
作为我的垂直间隔。如果我将它设置为 Minimum
或 MinimumExpanding
那么间隔不会随着调整大小而增长(也不会缩小)但它似乎已扩展到图 3 中的大小(损坏的间距在 QCheckBox
es 之间)。如果我将它设置为 Maximum
、Preferred
或 Expanding
,那么我会观察到与上述图 3 相同的行为。
在不影响元素间距的情况下,在一行网格中实现两个 QGroupBox
对齐的正确方法是什么(例如,在这种情况下,使垂直间隔器仅适合单行网格布局而不是 expand/shrink)?
如果两个 QGroupBox
具有相同数量的子项并且每行至少有一个子项具有 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>
我的表单的一部分包含 QGroupBox
(Status Box
),其中 4 个子 QGroupBox
es 以网格布局 (2x2) 排列。底部的两个 QGroupBox
es(Widget 1 Box
和 Widget 2 Box
)包含固定大小的小部件(设置 minimumSize
和 maximumSize
),因此它们根本无法调整大小在两个方向上。由于严格的大小限制,QGroupBox
es(Summary Box
和 Helper Box
)的顶行只能在垂直方向调整大小。
麻烦的部分来了。左上角 QGroupBox
(Summary Box
) 具有 5x3 网格布局,而右上角 (Helper Box
) 具有 6 行的垂直布局。如果我有如图 1 所示的天真的小部件放置,Qt 会放大顶行中两个标签的垂直尺寸以使两个 QGroupBox
es 的高度相等(参见图 1 上的红色箭头)。
这绝对是我不想要的,所以我在 Summary Box
的底部添加了垂直垫片,乍一看它起作用了(图 2)。但仅从第一眼看...您看到的是我整个表格的最小高度和垫片的底部以及 Helper Box
中的最后一个 QCheckBox
似乎对齐。
如果我垂直扩展表格,这个垫片会变大一点,这会导致两个顶部 QGroupBox
的高度增加。结果 QCheckBox
es 之间的间距也增加了,我们还可以看到右上角框的顶部和底部间距不相等(见图 3 中的红色箭头)。
我尝试使用 sizeType
作为我的垂直间隔。如果我将它设置为 Minimum
或 MinimumExpanding
那么间隔不会随着调整大小而增长(也不会缩小)但它似乎已扩展到图 3 中的大小(损坏的间距在 QCheckBox
es 之间)。如果我将它设置为 Maximum
、Preferred
或 Expanding
,那么我会观察到与上述图 3 相同的行为。
在不影响元素间距的情况下,在一行网格中实现两个 QGroupBox
对齐的正确方法是什么(例如,在这种情况下,使垂直间隔器仅适合单行网格布局而不是 expand/shrink)?
如果两个 QGroupBox
具有相同数量的子项并且每行至少有一个子项具有 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>