文章目录
- 1、QVBoxLayout
- 2、QHBoxLayout
- 3、QGridLayout
- 4、QFormLayout
- 5、QSpacerItem
布局管理器是为了让程序员不需要自己决定控件的绝对位置,而是通过布局管理器方便地放置
1、QVBoxLayout
垂直布局管理器
#include <QPushButton>
#include <QVBoxLayout>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);// 创建3个按钮, 用垂直布局管理器来管理QPushButton* button1 = new QPushButton("按钮1");QPushButton* button2 = new QPushButton("按钮2");QPushButton* button3 = new QPushButton("按钮3");QVBoxLayout* layout = new QVBoxLayout();layout->addWidget(button1);layout->addWidget(button2);layout->addWidget(button3);// 布局管理器添加到窗口中this->setLayout(layout);
}
运行后改变窗口,按钮尺寸也会跟着变化。
在界面直接操作控件的话,往里面放控件时也会自动找好位置,但是这样做好界面并运行后,变更界面大小,位于其中的控件不会跟着改变尺寸。
这是因为代码的方法是创建了layout,而直接在界面操作是先创建了一个Widget,再创建了一个layout。
当然,也可以先放一些控件,再把一个layout套在外面。
2、QHBoxLayout
水平布局管理器
#include <QPushButton>
#include <QHBoxLayout>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);QPushButton* button1 = new QPushButton("按钮1");QPushButton* button2 = new QPushButton("按钮2");QPushButton* button3 = new QPushButton("按钮3");QHBoxLayout* layout = new QHBoxLayout();layout->addWidget(button1);layout->addWidget(button2);layout->addWidget(button3);this->setLayout(layout);
}
布局管理器之间可以嵌套。
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);QVBoxLayout* vlayout = new QVBoxLayout();this->setLayout(vlayout);QPushButton* button1 = new QPushButton("按钮1");QPushButton* button2 = new QPushButton("按钮2");vlayout->addWidget(button1);vlayout->addWidget(button2);QHBoxLayout* hlayout = new QHBoxLayout();QPushButton* button3 = new QPushButton("按钮3");QPushButton* button4 = new QPushButton("按钮4");hlayout->addWidget(button3);hlayout->addWidget(button4);vlayout->addLayout(hlayout);
}
3、QGridLayout
网格布局
layoutColumnStretch是列方向的拉伸系数。
#include <QPushButton>
#include <QGridLayout>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);QPushButton* button1 = new QPushButton("按钮1");QPushButton* button2 = new QPushButton("按钮2");QPushButton* button3 = new QPushButton("按钮3");QPushButton* button4 = new QPushButton("按钮4");QGridLayout* layout = new QGridLayout();layout->addWidget(button1, 0, 0);layout->addWidget(button2, 0, 1);layout->addWidget(button3, 1, 2);layout->addWidget(button4, 2, 3);this->setLayout(layout);
}
通过addWidget也可以做到垂直或水平布局的样子。
layout->addWidget(button1, 0, 0);layout->addWidget(button2, 0, 1);layout->addWidget(button3, 1, 2);layout->addWidget(button4, 10, 3);
像这样,几个按钮的位置从坐标看相差甚远,但实际在界面中不会这样,这里设置的行数和列数,是控件之间的相对位置。
通过拉伸系数调节控件尺寸
#include <QPushButton>
#include <QGridLayout>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);QPushButton* button1 = new QPushButton("按钮1");QPushButton* button2 = new QPushButton("按钮2");QPushButton* button3 = new QPushButton("按钮3");QPushButton* button4 = new QPushButton("按钮4");QPushButton* button5 = new QPushButton("按钮5");QPushButton* button6 = new QPushButton("按钮6");QPushButton* button7 = new QPushButton("按钮7");QPushButton* button8 = new QPushButton("按钮8");QPushButton* button9 = new QPushButton("按钮9");QGridLayout* layout = new QGridLayout();layout->addWidget(button1, 0, 0);layout->addWidget(button2, 0, 1);layout->addWidget(button3, 0, 2);layout->addWidget(button4, 1, 0);layout->addWidget(button5, 1, 1);layout->addWidget(button6, 1, 2);layout->addWidget(button7, 2, 0);layout->addWidget(button8, 2, 1);layout->addWidget(button9, 2, 2);this->setLayout(layout);// 设置水平拉伸系数// 第一个参数表示第几列, 第二个参数表示拉伸系数// 此时三列的按钮的水平大小相比于原本的, 是2:2:4比例// 拉伸系数为0表示不拉伸layout->setColumnStretch(0, 1);layout->setColumnStretch(1, 1);layout->setColumnStretch(2, 2);// 设置垂直拉伸系数layout->setRowStretch(0, 1);layout->setRowStretch(1, 2);layout->setRowStretch(2, 2);
}
水平拉伸是能看出效果的,但是垂直拉伸效果是无效的,这是由SizePolicy决定的。
QSizePolicy::Ignored表示忽略控件大小,不对布局产生影响。
水平方向默认是拉开的,垂直则默认不拉伸,在设置拉伸系数之前解开这个限制即可。
button1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);button2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);button3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);button4->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);button5->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);button6->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);button7->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);button8->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);button9->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
整个部分
#include <QDebug>
#include <QPushButton>
#include <QGridLayout>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);QPushButton* button1 = new QPushButton("按钮1");QPushButton* button2 = new QPushButton("按钮2");QPushButton* button3 = new QPushButton("按钮3");QPushButton* button4 = new QPushButton("按钮4");QPushButton* button5 = new QPushButton("按钮5");QPushButton* button6 = new QPushButton("按钮6");QPushButton* button7 = new QPushButton("按钮7");QPushButton* button8 = new QPushButton("按钮8");QPushButton* button9 = new QPushButton("按钮9");button1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);button2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);button3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);button4->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);button5->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);button6->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);button7->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);button8->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);button9->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);QGridLayout* layout = new QGridLayout();layout->addWidget(button1, 0, 0);layout->addWidget(button2, 0, 1);layout->addWidget(button3, 0, 2);layout->addWidget(button4, 1, 0);layout->addWidget(button5, 1, 1);layout->addWidget(button6, 1, 2);layout->addWidget(button7, 2, 0);layout->addWidget(button8, 2, 1);layout->addWidget(button9, 2, 2);this->setLayout(layout);// 设置水平拉伸系数// 第一个参数表示第几列, 第二个参数表示拉伸系数// 此时三列的按钮的水平大小相比于原本的, 是2:2:4比例// 拉伸系数为0表示不拉伸layout->setColumnStretch(0, 1);layout->setColumnStretch(1, 1);layout->setColumnStretch(2, 2);// 设置垂直拉伸系数layout->setRowStretch(0, 1);layout->setRowStretch(1, 2);layout->setRowStretch(2, 2);
}
4、QFormLayout
表单布局
#include <QFormLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);QFormLayout* layout = new QFormLayout();this->setLayout(layout);QLabel* label1 = new QLabel("姓名");QLabel* label2 = new QLabel("年龄");QLabel* label3 = new QLabel("电话");QLineEdit* edit1 = new QLineEdit();QLineEdit* edit2 = new QLineEdit();QLineEdit* edit3 = new QLineEdit();layout->addRow(label1, edit1);layout->addRow(label2, edit2);layout->addRow(label3, edit3);QPushButton* button = new QPushButton("提交");layout->addRow(nullptr, button);
}
5、QSpacerItem
控件之间的空白
#include <QHBoxLayout>
#include <QPushButton>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);QHBoxLayout* layout = new QHBoxLayout();this->setLayout(layout);QPushButton* button1 = new QPushButton("按钮1");QPushButton* button2 = new QPushButton("按钮2");QSpacerItem* spacer = new QSpacerItem(40, 70);// 空白在控件中间, 那么代码顺序中添加空白的代码就写在中间layout->addWidget(button1);layout->addSpacerItem(spacer);layout->addWidget(button2);
}
结束。