为什么Qt Designer要加那么多space?

Why does Qt Designer add so much space?

为什么Qt能赚那么多space?我怎样才能解决这个问题?我只想创建两个标签、两个文本框和一个登录按钮。我正在尝试创建一个登录表单。

像这样:

为什么需要那么多 space 才能有小按钮?

这是我能看到的最好的,但即使这样看起来也很糟糕。

只需在顶部和底部添加一个Vertical Spacer,您就会得到预期的结果。

If you would like to add it through the code and not in Designer, you would need to add it on the QLayout with QBoxLayout::addStretch(int stretch = 0) or QBoxLayout::addSpacing(int size), depending on your need

你想要这样的东西吗? example 我猜你可以使用 Qt Designer 中的间隔符来实现你想要的。

下面是我的示例的 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>683</width>
                <height>408</height>
            </rect>
        </property>
        <property name="windowTitle">
            <string>MainWindow</string>
        </property>
        <widget class="QWidget" name="centralwidget">
            <layout class="QGridLayout" name="gridLayout">
                <item row="1" column="2">
                    <spacer name="horizontalSpacer_2">
                        <property name="orientation">
                            <enum>Qt::Horizontal</enum>
                        </property>
                        <property name="sizeHint" stdset="0">
                            <size>
                                <width>40</width>
                                <height>20</height>
                            </size>
                        </property>
                    </spacer>
                </item>
                <item row="1" column="1">
                    <widget class="QGroupBox" name="groupBox">
                        <property name="title">
                            <string>GroupBox</string>
                        </property>
                        <layout class="QGridLayout" name="gridLayout_2">
                            <item row="1" column="0">
                                <widget class="QLabel" name="label_2">
                                    <property name="text">
                                        <string>TextLabel</string>
                                    </property>
                                </widget>
                            </item>
                            <item row="0" column="0">
                                <widget class="QLabel" name="label">
                                    <property name="text">
                                        <string>TextLabel</string>
                                    </property>
                                </widget>
                            </item>
                            <item row="2" column="0">
                                <spacer name="horizontalSpacer_3">
                                    <property name="orientation">
                                        <enum>Qt::Horizontal</enum>
                                    </property>
                                    <property name="sizeHint" stdset="0">
                                        <size>
                                            <width>40</width>
                                            <height>20</height>
                                        </size>
                                    </property>
                                </spacer>
                            </item>
                            <item row="2" column="1">
                                <widget class="QPushButton" name="pushButton">
                                    <property name="text">
                                        <string>PushButton</string>
                                    </property>
                                </widget>
                            </item>
                            <item row="0" column="1">
                                <widget class="QLineEdit" name="lineEdit" />
                            </item>
                            <item row="1" column="1">
                                <widget class="QLineEdit" name="lineEdit_2" />
                            </item>
                            <item row="2" column="2">
                                <spacer name="horizontalSpacer_4">
                                    <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>
                    </widget>
                </item>
                <item row="1" column="0">
                    <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>
                <item row="0" column="1">
                    <spacer name="verticalSpacer">
                        <property name="orientation">
                            <enum>Qt::Vertical</enum>
                        </property>
                        <property name="sizeHint" stdset="0">
                            <size>
                                <width>20</width>
                                <height>40</height>
                            </size>
                        </property>
                    </spacer>
                </item>
                <item row="2" column="1">
                    <spacer name="verticalSpacer_2">
                        <property name="orientation">
                            <enum>Qt::Vertical</enum>
                        </property>
                        <property name="sizeHint" stdset="0">
                            <size>
                                <width>20</width>
                                <height>40</height>
                            </size>
                        </property>
                    </spacer>
                </item>
            </layout>
        </widget>
        <widget class="QMenuBar" name="menubar">
            <property name="geometry">
                <rect>
                    <x>0</x>
                    <y>0</y>
                    <width>683</width>
                    <height>21</height>
                </rect>
            </property>
        </widget>
        <widget class="QStatusBar" name="statusbar" />
    </widget>
    <resources />
    <connections />
</ui>

此外,给你一个快速提示,如果你是初学者,我认为当你不需要两个布局时使用两个布局是错误的:在你的例子中,删除“formLayout”并格式化你的“LoginScreen”改为 QFormLayout。