1.布局管理器介绍
打开Qt Creator设计师界面,左边导航栏第一栏就是布局管理器,如下图所示:
布局管理器的主要功能是将界面中的各个控件按一定的布局将组件整合起来。Qt 设计师中的Layouts如上图有4种:
- 垂直布局(Vertical Layout)
- 水平布局(Horizontal Layout)
- 网格布局(Grid Layout)
- 表单布局(Form Layout)
Layouts | 所属类 |
垂直布局 | QVBoxLayout |
水平布局 | QHBoxLayout |
网格布局 | QGridLayout |
表单布局 | QFormLayout |
2.布局管理器属性
如上图所示,属性所代表的含义分别是:
layoutName | 布局名称 |
layoutLeftMargin | 距左边框内的距离 |
layoutTopMargin | 距上边框内的距离 |
layoutRightMargin | 距右边框内的距离 |
layoutBottomMargin | 距底边距内的距离 |
layoutSpacing | 布局内部控件之间的间隙 |
layoutStretch | 布局内各个控件所占的比例 |
layoutSizeConstraint | 布局大小约束 |
其中 layoutSizeConstraint 元素及含义:
3.代码示例:
QHBoxLayout *horizontalLayout;
horizontalLayout = new QHBoxLayout(horizontalLayoutWidget);
horizontalLayout->setSpacing(6);
horizontalLayout->setContentsMargins(11, 11, 11, 11);
horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));