1. QHBoxLayout
Widget::Widget(QWidget *parent)
: QWidget(parent) {
QPushButton *button1 = new QPushButton("One");
QPushButton *button2 = new QPushButton("Two");
QPushButton *button3 = new QPushButton("Three");
QPushButton *button4 = new QPushButton("Four");
QPushButton *button5 = new QPushButton("Five");
QHBoxLayout *layout = new QHBoxLayout(this);
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->addWidget(button5);
}
Widget::~Widget() {
}
2. HVBoxLayout
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent) {
QPushButton *button1 = new QPushButton("One");
QPushButton *button2 = new QPushButton("Two");
QPushButton *button3 = new QPushButton("Three");
QPushButton *button4 = new QPushButton("Four");
QPushButton *button5 = new QPushButton("Five");
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->addWidget(button5);
setLayout(layout); // 如果layout构造函数使用this,就可以省略这句代码
}
Widget::~Widget() {
}
3. QHBoxLayout 和 QVBoxLayout 复合布局
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent) {
QPushButton *button1 = new QPushButton("One");
QPushButton *button2 = new QPushButton("Two");
QPushButton *button3 = new QPushButton("Three");
QPushButton *button4 = new QPushButton("Four");
QPushButton *button5 = new QPushButton("Five");
QHBoxLayout *layout = new QHBoxLayout(this);
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->addWidget(button5);
QPushButton *button11 = new QPushButton("1");
QPushButton *button22 = new QPushButton("2");
QPushButton *button33 = new QPushButton("3");
QPushButton *button44 = new QPushButton("4");
QPushButton *button55 = new QPushButton("5");
QVBoxLayout *vLayout = new QVBoxLayout();
vLayout->addWidget(button11);
vLayout->addWidget(button22);
vLayout->addWidget(button33);
vLayout->addWidget(button44);
vLayout->addWidget(button55);
layout->addLayout(vLayout);
}
Widget::~Widget() {
}
- 设定控件间固定的间距
// button2 和 button3之间间距100
layout->addWidget(button1);
layout->addWidget(button2);
layout->addSpacing(100);
layout->addWidget(button3);
layout->addWidget(button4);
layout->addWidget(button5);
- 控件间可以*拉伸
// 拉伸整体窗口的时候是在button2 和 button3之间可以*拉伸
layout->addWidget(button1);
layout->addWidget(button2);
layout->addStretch();
layout->addWidget(button3);
layout->addWidget(button4);
layout->addWidget(button5);
- 间隔一定距离基础上进行缩小或者拉伸
// button4 和 button5 之间默认间隔距离100,随着整体窗口的拉伸,这个距离可以进一步拉伸且不会影响其他按钮的大小
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->addSpacerItem(new QSpacerItem(100,100, QSizePolicy::MinimumExpanding));
layout->addWidget(button5);
默认大小
拉伸
4. QFormLayout
QLineEdit *nameLineEdit = new QLineEdit();
QLineEdit *emailLineEdit = new QLineEdit();
QLineEdit *ageSpinBox = new QLineEdit();
QFormLayout *formLayout = new QFormLayout;
formLayout->addRow(tr("&Name:"), nameLineEdit);
formLayout->addRow(tr("&Email:"), emailLineEdit);
formLayout->addRow(tr("&Age:"), ageSpinBox);
// formLayout->setFieldGrowthPolicy(QFormLayout::FieldGrowthPolicy::AllNonFixedFieldsGrow); // 这段代码可以对QLineEdit这一列进行拉伸
setLayout(formLayout);
5. QGridLayout
QLineEdit *nameLineEdit = new QLineEdit();
QLineEdit *emailLineEdit = new QLineEdit();
QLineEdit *ageSpinBox = new QLineEdit();
QLabel *nameLabel = new QLabel(tr("&Name:"));
nameLabel->setBuddy(nameLineEdit);
QLabel *emailLabel = new QLabel(tr("&Name:"));
emailLabel->setBuddy(emailLineEdit);
QLabel *ageLabel = new QLabel(tr("&Name:"));
ageLabel->setBuddy(ageSpinBox);
QGridLayout *gridLayout = new QGridLayout;
gridLayout->addWidget(nameLabel, 0, 0);
gridLayout->addWidget(nameLineEdit, 0, 1);
gridLayout->addWidget(emailLabel, 1, 0);
gridLayout->addWidget(emailLineEdit, 1, 1);
gridLayout->addWidget(ageLabel, 2, 0);
gridLayout->addWidget(ageSpinBox, 2, 1);
setLayout(gridLayout);
6. QStackedLayout
QWidget *firstPageWidget = new QWidget;
QLabel *firstLabel = new QLabel("first widget", firstPageWidget);
QWidget *secondPageWidget = new QWidget;
QLabel *secondLabel = new QLabel("second widget", secondPageWidget);
QWidget *thirdPageWidget = new QWidget;
QLabel *thirdLabel = new QLabel("third widget", thirdPageWidget);
QStackedLayout *stackedLayout = new QStackedLayout;
stackedLayout->addWidget(firstPageWidget);
stackedLayout->addWidget(secondPageWidget);
stackedLayout->addWidget(thirdPageWidget);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(stackedLayout);
setLayout(mainLayout);
stackedLayout->setCurrentIndex(0); // 设置 firstPageWidget为当前显示的widget
//stackedLayout->setCurrentIndex(1); // 设置 secondPageWidget为当前显示的widget
//stackedLayout->setCurrentIndex(2); // 设置 thirdPageWidget为当前显示的widget