QLayout窗口布局

 

介绍

QLayout

Header:#include <QLayout>
qmake:QT += widgets
Inherits:QObject and QLayoutItem
Inherited By:QBoxLayout, QFormLayout, QGridLayout, and QStackedLayout

涉及到的控件主要有:QSplitter窗口分割器、QSpacerItem间距控制(类似于弹簧效果)、QHBoxLayout(1行n列)和QVBoxLayout(n行1列)行列布局、QFormLayout表单布局(n行2列)、QGridLayout栅格布局(n行n列)

addWidget(QWidget *w)、removeWidget(QWidget *widget)? QWidget操作

setSpacing(int spacing) setHorizontalSpacing(int spacing) setVerticalSpacing(int spacing)设置间距

QSpacerItem

在使用Designer时,就是Spacers里面的行列spacer,弹簧样式的图标,此控件添加以后不会在界面显示,主要是占位使用。任何layout默认是先符合控件的sizePolicy的要求下进行控件大小、间距调整。

但如果想要实现类似于程序标题栏的效果,左侧图标、程序名,右侧最大化、最小化关闭按钮,中间就需要一个占位的空白控件,这时候需要使用QSpacerItem。

在Designer时,直接拖拽到需要占位的地方(注意,两个空间之间或者布局之间均可,但其所在空间必须是QLayout而不是QWidget)

代码使用:使用addSpacerItem(QSpacerItem *spacerItem)、insertSpacerItem(int index, QSpacerItem *spacerItem)、removeItem(QLayoutItem *item)

addSpacing(int size)这类方法是设置间距而不是插入spaceritem

spacerItem父类是QLayoutItem,直接removeQLayoutItem 即可删除,同理可以使用removeItem(QLayoutItem *item)、

QHBoxLayout、QVBoxLayout

其父类为QBoxLayout,可以配合QSpacerItem使用

QFormLayout

n行两列表单,提供了一套insertRowremoveRowaddRow的方法,此类默认第一列为QLabel,支持第一列只提供字符串而不提供QLabel对象

表单换行策略

setRowWrapPolicy(RowWrapPolicy policy)

ConstantValueDescription
QFormLayout::DontWrapRows0一直在一行Fields are always laid out next to their label. This is the default policy for all styles except Qt Extended styles.
QFormLayout::WrapLongRows1自适应,如果空间不够则两行Labels are given enough horizontal space to fit the widest label, and the rest of the space is given to the fields. If the minimum size of a field pair is wider than the available space, the field is wrapped to the next line. This is the default policy for Qt Extended styles.
QFormLayout::WrapAllRows2一直两行Fields are always laid out below their label.

setWidget(int row, ItemRole role, QWidget *widget)

不使用addrow一类的整行添加,也可以逐个添加,使用此函数需要设置ItemRole

ConstantValueDescription
QFormLayout::LabelRole0标签列A label widget.
QFormLayout::FieldRole1输入框列A field widget.
QFormLayout::SpanningRole2单控件占用整行A widget that spans label and field columns.

QGridLayout

适用于复杂布局

void addLayout(QLayout *layout, int row, int column, Qt::Alignment alignment = Qt::Alignment())
void addLayout(QLayout *layout, int row, int column, int rowSpan, int columnSpan, Qt::Alignment alignment = Qt::Alignment())
void addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment = Qt::Alignment())
void addWidget(QWidget *widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = Qt::Alignment())

注意row,column为起点位置,rowSpan,columnSpan表示占用的行数,类似于excel中的合并单元格效果

void setColumnStretch(int column, int stretch)
void setRowStretch(int row, int stretch)
void setHorizontalSpacing(int spacing)
void setVerticalSpacing(int spacing)

设置伸缩空间和间距

QStackedLayout

堆布局,这个布局实现widget分页显示,Qt提供了他的一个控件QStackedWidget

QSplitter

实现窗口分割效果,且可以动态调整比例

setOpaqueResize(bool opaque = true) 在调整比例时是否动态更新

setChildrenCollapsible(bool) 是否允许子窗口为0尺寸

addWidget(QWidget *widget)、insertWidget(int index, QWidget *widget) 添加窗口

注意是窗口分割 不是布局分割,所以不能支持布局的添加

Techie亮博客,转载请注明:Coologic » QLayout窗口布局

Coologic 博客域名已更改,已从 www.techieliang.com 改为 www.coologic.cn,上述链接地址受到影响,若需查看源文请手动修改,多有不便,敬请谅解。
### QT 窗口布局使用方法 在 Qt 中,窗口布局通过 `QLayout` 类及其子类实现。这些布局管理器可以自动调整控件的位置和大小,从而适应不同屏幕尺寸或分辨率的变化。以下是关于如何使用 Qt 布局系统的详细介绍。 #### 1. **基本概念** Qt 的布局系统由多个类组成,主要包括以下几种常见的布局方式: - 水平布局 (`QHBoxLayout`):将控件按水平方向排列。 - 垂直布局 (`QVBoxLayout`):将控件按垂直方向排列。 - 网格布局 (`QGridLayout`):将控件放置在一个二维表格中。 - 表单布局 (`QFormLayout`):适用于表单样式的界面设计。 每种布局都有其适用场景,开发者可以根据实际需求选择合适的布局方案[^1]。 #### 2. **设置外边距** 默认情况下,Qt 控件的外边距为 0。如果需要自定义边距,可以通过以下函数来设置: ```cpp setContentsMargins(int left, int top, int right, int bottom); ``` 或者更简洁的方式: ```cpp setContentsMargins(QMargins margins); ``` 这允许开发人员灵活控制窗口内部空间的设计[^3]。 #### 3. **示例代码** 下面是一个简单的例子,展示如何在 Qt 应用程序中应用不同的布局: ```cpp #include <QApplication> #include <QWidget> #include <QPushButton> #include <QVBoxLayout> #include <QHBoxLayout> #include <QGridLayout> int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget window; window.setWindowTitle("Qt Layout Example"); // 创建按钮实例 QPushButton* button1 = new QPushButton("Button 1"); QPushButton* button2 = new QPushButton("Button 2"); QPushButton* button3 = new QPushButton("Button 3"); QPushButton* button4 = new QPushButton("Button 4"); // 设置垂直布局 QVBoxLayout* vBox = new QVBoxLayout; // 添加按钮到垂直布局 vBox->addWidget(button1); vBox->addWidget(button2); // 设置水平布局 QHBoxLayout* hBox = new QHBoxLayout; // 将垂直布局嵌套到水平布局中 hBox->addLayout(vBox); // 继续向水平布局添加其他控件 hBox->addWidget(button3); hBox->addWidget(button4); // 设置网格布局作为顶层布局 QGridLayout* gridLayout = new QGridLayout; gridLayout->addLayout(hBox, 0, 0); // 设置窗口的内容区域并指定布局 window.setLayout(gridLayout); // 显示窗口 window.show(); return app.exec(); } ``` 此代码展示了如何组合使用多种布局类型(如 `QVBoxLayout`, `QHBoxLayout`, 和 `QGridLayout`),并通过嵌套实现复杂的 UI 设计[^1]。 #### 4. **注意事项** - 如果未显式设置任何布局,则当父窗口调整大小时,其中的控件不会随之改变位置或大小。 - 可以通过调用 `setLayout()` 方法将某个布局附加到容器部件上。 - 对于复杂的应用程序结构,建议合理规划层次化的布局体系,以便维护清晰性和可扩展性。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值