Qt提供了以下几种布局容器
QHBoxLayout:横向布局 , 继承自QBoxLayout,QBoxLayout继承自QLayout
QVBoxLayout:纵向布局 , 继承自QBoxLayout
QGridLayout:表格布局 , 继承自QLayout
QFormLayout:表单布局 ,继承自QLayout
学习使用QHBoxLayout布局容器
setSpacing设置元素间隔,参数整型值
setMargin设置边距,是个整型值
addStretch弹簧主要作用是占位,参数整形值,占位系数
QHBoxLayout成员方法:
void addWidget(QWidget *, int stretch = 0, Qt::Alignment alignment = Qt::Alignment());
三个参数:
参数1:窗口指针
参数2:窗口size拉伸系数(具有更高拉伸系数的部件会长得更多)
参数3:对齐方式,上下左右居中等.......
The horizontal flags are:
Constant Value Description
Qt::AlignLeft 0x0001 靠左边排列
Qt::AlignRight 0x0002 靠右边排列
Qt::AlignHCenter 0x0004 水平居中排列
Qt::AlignJustify 0x0008 靠左右两边排列
The vertical flags are:
ConstantValueDescription
Qt::AlignTop 0x0020 靠顶部排列
Qt::AlignBottom 0x0040 靠下边排列
Qt::AlignVCenter 0x0080 垂直居中排列
Qt::AlignBaseline 0x0100 与基线对齐排列
You can use only one of the horizontal flags at a time.
There is one two-dimensional flag:
Constant Value Description
Qt::AlignCenter AlignVCenter | AlignHCenter 居中排列
创建12个彩色背景的QWidget,添加进QHBoxLayout布局容器里,便于测试
参数2和参数3使用了默认值
#include "widget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QFormLayout>
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
setMinimumSize(600,400);
QHBoxLayout* h = new QHBoxLayout(

本文详细介绍了Qt中的QHBoxLayout布局管理器的使用方法,包括设置元素间隔、边距、拉伸系数和对齐方式。通过实例展示了如何创建和添加彩色背景的QWidget,以及如何调整不同部件的拉伸比例和对齐方式,以实现更灵活的界面布局。同时,还探讨了通过添加子布局来实现更复杂的布局结构。
最低0.47元/天 解锁文章
954

被折叠的 条评论
为什么被折叠?



