第五章布局管理组(Layouts)
1.盒子布局(BoxLayout)
#include "Widget.h"
#include <QBoxLayout>
#include <QCheckBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QRadioButton>
Widget::Widget(QWidget* parent)
: QWidget(parent)
{
auto btn1 = new QPushButton("按钮1");
auto btn2 = new QPushButton("按钮2");
auto lab1 = new QLabel;
auto lab2 = new QLabel;
lab1->setPixmap(QPixmap(R"(C:\Users\PVer\Pictures\Resource\松鼠.bmp)"));
lab2->setPixmap(QPixmap(R"(C:\Users\PVer\Pictures\Resource\xiaoku.png)"));
auto ledit1 = new QLineEdit;
auto ledit2 = new QLineEdit;
auto boxLayout = new QBoxLayout(QBoxLayout::TopToBottom, this);
auto hbLayout = new QHBoxLayout;
auto vbLayout = new QVBoxLayout;
hbLayout->addWidget(lab1);
hbLayout->addWidget(btn1);
hbLayout->addWidget(ledit1);
vbLayout->addWidget(lab2);
vbLayout->addWidget(ledit2);
boxLayout->addLayout(hbLayout);
boxLayout->addLayout(vbLayout);
boxLayout->addSpacing(50);
hbLayout->addStretch(1);
boxLayout->addWidget(btn2, 2);
boxLayout->setStretchFactor(btn2, 2);
boxLayout->addWidget(btn2, 2, Qt::AlignTop);
boxLayout->setDirection(QBoxLayout::LeftToRight);
boxLayout->setContentsMargins(10, 20, 30, 40);
this->setLayout(boxLayout);
}
Widget::~Widget() { }
2.网格布局(GridLayout)
auto glayout = new QGridLayout(this);
glayout->addWidget(lab1, 0, 0, 2, 2);
glayout->addWidget(ledit1, 0, 2);
glayout->addWidget(ledit2, 1, 2);
glayout->addWidget(btn1, 2, 0, 1, 2);
glayout->addLayout(vbLayout, 3, 0);
glayout->setRowStretch(2, 3);
glayout->setColumnStretch(1, 2);
glayout->setColumnMinimumWidth(3, 200);
glayout->setRowMinimumHeight(1, 100);
3.表单布局(FormLayout)
#include <QFormLayout>
#include <QRadioButton>
QHBoxLayout* sexLayout = new QHBoxLayout;
sexLayout->addWidget(new QRadioButton("男"));
sexLayout->addWidget(new QRadioButton("女"));
auto fLayout = new QFormLayout(this);
fLayout->addRow("姓名", ledit1);
fLayout->addRow("性别", sexLayout);
fLayout->addRow(lab1);
fLayout->setRowWrapPolicy(QFormLayout::WrapAllRows);
fLayout->setRowWrapPolicy(QFormLayout::WrapLongRows);
fLayout->setSpacing(50);
fLayout->setWidget(0, QFormLayout::LabelRole, lab2);
fLayout->insertRow(0, lab2);
4.堆栈布局(StackedLayout)
#include <QStackedLayout>
auto sLayout = new QStackedLayout;
QWidget* w1 = new QWidget;
QHBoxLayout* hl = new QHBoxLayout;
hl->addWidget(btn1);
hl->addWidget(ledit1);
w1->setLayout(hl);
QWidget* w2 = new QWidget;
QVBoxLayout* vl = new QVBoxLayout;
vl->addWidget(lab2);
vl->addWidget(btn2);
w2->setLayout(vl);
QWidget* w3 = new QWidget;
QVBoxLayout* vl1 = new QVBoxLayout;
QPushButton* btn3 = new QPushButton("按钮3");
vl1->addWidget(btn3);
w3->setLayout(vl1);
w3->show();
w3->move(10, 10);
sLayout->addWidget(w1);
sLayout->addWidget(w2);
setLayout(sLayout);
connect(btn1, &QPushButton::clicked, [=] {
sLayout->setCurrentIndex(1);
});
connect(btn2, &QPushButton::clicked, [=] {
sLayout->setCurrentIndex(0);
});
connect(btn3, &QPushButton::clicked, [=] {
qInfo() << sLayout->count();
if (sLayout->count() > 1) {
sLayout->removeWidget(w2);
btn3->setText("插入w2");
} else {
sLayout->insertWidget(1, w2);
btn3->setText("删除w2");
}
});
connect(sLayout, &QStackedLayout::currentChanged, [] { qInfo() << "界面改变了"; });
connect(sLayout, &QStackedLayout::widgetRemoved, [] { qInfo() << "界面被删除了"; });
sLayout->setStackingMode(QStackedLayout::StackAll);
5.分割器(Splitter)
#include <QSplitter>
#include <QPlainTextEdit>
auto sp = new QSplitter(this);
sp->addWidget(new QPlainTextEdit);
sp->addWidget(new QPlainTextEdit);
sp->insertWidget(2, btn2);
connect(sp, &QSplitter::splitterMoved, [](int pos, int index) {
qInfo() << "当前移动位置=" << pos << "是第几个分割器在移动=" << index;
});
sp->setCollapsible(2, false);
QWidget补充
setWindowIcon(QIcon(R"(C:\Users\PVer\Pictures\Resource\xiaoku.png)"));
setWindowTitle("布局管理器");
resize(600, 400);
setMinimumSize(200, 200);
btn2->setVisible(false);
btn2->show();
connect(btn2, &QPushButton::clicked, [=] {
QPixmap pix = this->grab();
pix.save("./pix.jpg");
});