Qt Designer的布局不遵循预定义几何形状
我正在使用Qt Designer创建一个新的QWidget
,里面有一个frame
,这个框架里包含了一个QLabel
和一个QTextEdit
。我可以设置这些元素的大小,并把它们设为fixed
,这样它们的高度和宽度就不会改变:
在这个例子中,我的QLabel
和QLineEdit
位于它们父框架的右上角。如果我现在给这个框架设置一个布局(这是为了确保在调整窗口大小时,元素能保持在正确的位置,或者如果这个框架是QScrollArea
的主控件),那么这些元素的位置就会被移动,即使是在框架内部:
在这个情况下,我给框架应用了一个表单布局;我们可以看到QLabel
的坐标从(0,0)变成了(10,10)。无论我之前是否把这两个元素放在一个垂直框架里,这种情况都会发生。
因为我想通过手动设置元素的坐标来创建一个非常精确的布局,但在应用布局时它们却随机移动,这让我感到非常沮丧。我该怎么做呢?!我想我可能误解了什么,但我不知道是什么!
3 个回答
布局的作用就是控制里面元素的大小和位置。这是它们的工作原理。如果布局不控制这些小部件的大小和形状,当窗口大小改变时,它们该怎么办呢?
与其死死地控制每个东西的大小和位置,不如利用布局来表达设计的意图:比如这两个控件是要放在一起的,这些相同的项目应该排成一列,等等。
影响位置和大小的关键因素有几个,你需要特别注意:
- 你基础表单的布局
- 表单内部的布局
- 控件的大小策略以及控件的最小和最大大小值
这些因素是相互关联的,像瀑布一样层层叠加。通常需要多次尝试和按CTRL+R来查看表单的外观和调整大小,但基本上就是这三样东西。
布局有默认的边距和间距,这些具体的数值是由当前的控件样式决定的。
你可以通过点击布局的父控件,然后向下滚动到属性编辑器的底部,找到一个布局部分,在那里你会看到相关的数值(-1
表示“使用默认值”)。
编辑:
我之前没有说明的一点是,每次你打断并重置布局时,布局的边距和间距都会恢复到默认值!这有时候会让人很烦,但其实是有道理的,因为每次这样做都会创建一个新的布局。
不过,我觉得问题的真正来源是你在使用表单布局,这种布局的灵活性比其他布局要差很多。我建议你把标签和输入框放在一个水平布局中,并在最后加一个水平间隔。然后在这个水平布局下面放一个垂直间隔,再在框架中添加一个垂直布局。这样安排可以让标签和输入框保持在框架的左上角。设置好这些后,你就可以把垂直布局的边距设置为零,以达到想要的效果。
这里有一个 .ui 文件,你可以测试一下,看看这些内容是如何组合在一起的:
<?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>316</width>
<height>150</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QFrame" name="frame">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>75</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>106</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>