為什么 Qt 會騰出這么多空間?我怎樣才能解決這個問題?我只想創建兩個標簽、兩個文本框和一個登錄按鈕。我正在嘗試制作登錄表單。

像這樣的東西:

為什么只有小按鈕需要這么大的空間?

這是我能夠讓它看起來最好的,但即使這樣看起來也很糟糕。
uj5u.com熱心網友回復:
只需Vertical Spacer在頂部和底部添加一個,然后您將獲得預期的結果。
如果您想通過代碼而不是在 Designer 中添加它,您需要使用
QBoxLayout::addStretch(int stretch = 0)或將其添加到 QLayout 中QBoxLayout::addSpacing(int size),具體取決于您的需要
uj5u.com熱心網友回復:
你想要那樣的東西嗎?示例 您可以使用 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 代替。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/414846.html
標籤:
上一篇:QLineEdit:無法顯示
