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();//创建横向布局容器对象h
setLayout(h);//h设置为窗体的布局容器
constexpr int sz = 12;
QString clrs[sz]{//颜色集合
"red","green","blue","yellow","pink","lightGray",
"black","slateBlue","darkgoldenrod","darkcyan","chartreuse","gold"
};
for(int i = 0; i<sz ;++i)//创建12个彩色窗体
{
QWidget* widget = new QWidget();//创建窗体
QString ss = "background-color:"+clrs[i];
widget->setStyleSheet(ss);//设置窗体背景色
h->addWidget(widget);//将窗体添加到布局容器h里,参数2和参数3使用默认值
}
setWindowTitle(QString::number(h->count()));//布局容器管理的布局对象数量
}
效果图:
参数2:设置了拉伸系数1,2,3不同值
参数3:使用了默认值
QVector<QWidget*> widgets;
for(int i = 0; i<sz ;++i)
{
QWidget* widget = new QWidget();
QString ss = "background-color:"+clrs[i];
widget->setStyleSheet(ss);
widgets.push_back(widget);
// h->addWidget(widget);
}
//设置不同拉伸系数
h->addWidget(widgets[0],1);
h->addWidget(widgets[1],2);
h->addWidget(widgets[2],3);
h->addWidget(widgets[3],1);
h->addWidget(widgets[4],2);
h->addWidget(widgets[5],3);
效果图:
设置参数3:
QVector<QWidget*> buttons;
for(int i = 0; i<sz ;++i)
{
QPushButton* button = new QPushButton(QString::number(i+1));
QString ss = "background-color:"+clrs[i];
button->setStyleSheet(ss);
buttons.push_back(button);
// h->addWidget(widget);
}
QHBoxLayout* h = new QHBoxLayout(this);
setLayout(h);
// h->setAlignment(Qt::AlignTop);
h->addWidget(buttons[0],1,Qt::AlignBottom);
h->addWidget(buttons[1],2,Qt::AlignLeft);
h->addWidget(buttons[2],3,Qt::AlignTop);
效果图:
按钮1设置Qt::AlignBottom,靠下显示,拉伸会改变大小,比例是1
按钮2设置Qt::AlignLeft,靠左显示,拉伸不会改变大小
但是占据窗口的比例系数是2,按钮2到按钮3的空白区,是按钮2所有
按钮3设置Qt::AlignTop,靠上显示,拉伸会改变大小,比例是3
QHBoxLayout成员方法:
void addLayout(QLayout *layout, int stretch = 0);
2个参数:
参数1:是一个布局容器指针
参数2:是拉伸系数
QVector<QWidget*> buttons;
for(int i = 0; i<sz ;++i)
{
QPushButton* button = new QPushButton(QString::number(i+1));
QString ss = "background-color:"+clrs[i];
button->setStyleSheet(ss);
buttons.push_back(button);
// h->addWidget(widget);
}
QHBoxLayout* h = new QHBoxLayout(this);
setLayout(h);
QHBoxLayout* h1 = new QHBoxLayout();
h1->addWidget(buttons[0],1,Qt::AlignBottom);
h1->addWidget(buttons[1],2,Qt::AlignLeft);
h1->addWidget(buttons[2],3,Qt::AlignTop);
QHBoxLayout* h2 = new QHBoxLayout();
h2->addWidget(buttons[3],1,Qt::AlignLeft);
h2->addWidget(buttons[4],2);
h2->addWidget(buttons[5],3,Qt::AlignTop);
h2->addWidget(buttons[6],1,Qt::AlignBottom);
h->addLayout(h1);
h->addLayout(h2);
没有向h布局容器中添加任何窗口,而是使用了addLayout添加了两个布局容器,容器中存放容器,这样可以进行更加灵活的布局
效果图: