常用的三种布局方法:
水平布局类QHBoxLayout
垂直布局类QVBoxLayout
网格布局类QGridLayout
头文件:
include <QHBoxLayout>
include <QVBoxLayout>
include <QGridLayout>
水平布局:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *w = new QWidget;
QPushButton* button00 = new QPushButton("0");
QPushButton* button01 = new QPushButton("1");
QPushButton* button02 = new QPushButton("2");
QPushButton* button03 = new QPushButton("3");
QPushButton* button04 = new QPushButton("4");
QHBoxLayout* layout = new QHBoxLayout();
layout->addWidget(button00);
layout->addWidget(button01);
layout->addWidget(button02);
layout->addWidget(button03);
layout->addWidget(button04);
w->setLayout(layout);
w->show();
return a.exec();
}
效果图:
1.jpg垂直布局:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *w = new QWidget;
QPushButton* button00 = new QPushButton("0");
QPushButton* button01 = new QPushButton("1");
QPushButton* button02 = new QPushButton("2");
QPushButton* button03 = new QPushButton("3");
QPushButton* button04 = new QPushButton("4");
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(button00);
layout->addWidget(button01);
layout->addWidget(button02);
layout->addWidget(button03);
layout->addWidget(button04);
w->setLayout(layout);
w->show();
return a.exec();
}
效果图:
2.jpg网格布局:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *w = new QWidget;
QPushButton* button00 = new QPushButton("0");
QPushButton* button01 = new QPushButton("1");
QPushButton* button02 = new QPushButton("2");
QPushButton* button03 = new QPushButton("3");
QPushButton* button04 = new QPushButton("4");
QPushButton* button05 = new QPushButton("5");
QPushButton* button06 = new QPushButton("6");
QPushButton* button07 = new QPushButton("7");
QPushButton* button08 = new QPushButton("8");
QPushButton* button09 = new QPushButton("9");
QGridLayout* layout = new QGridLayout();
//QGridLayout需指定控件位置,以0开始
layout->addWidget(button00, 0, 0);
layout->addWidget(button01, 0, 1);
layout->addWidget(button02, 0, 2);
layout->addWidget(button03, 1, 0);
layout->addWidget(button04, 1, 1);
layout->addWidget(button05, 1, 2);
layout->addWidget(button06, 2, 0);
layout->addWidget(button07, 2, 1);
layout->addWidget(button08, 2, 2);
//addWidget的重载,可理解成:从第4行第一位开始,占用3个位置
layout->addWidget(button09, 3, 0, 3, 3);
w->setLayout(layout);
w->show();
return a.exec();
}
效果图:
3.jpg嵌套布局:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *w = new QWidget;
QPushButton* button00 = new QPushButton("0");
QPushButton* button01 = new QPushButton("1");
QPushButton* button02 = new QPushButton("2");
QPushButton* button03 = new QPushButton("3");
QPushButton* button04 = new QPushButton("4");
QPushButton* button05 = new QPushButton("5");
QPushButton* button06 = new QPushButton("6");
QHBoxLayout* layoutH = new QHBoxLayout();
QVBoxLayout* layoutV = new QVBoxLayout();
layoutH->addWidget(button00);
layoutH->addWidget(button01);
layoutH->addWidget(button02);
layoutH->addWidget(button03);
layoutV->addWidget(button04);
layoutV->addWidget(button05);
layoutV->addWidget(button06);
//将纵向布局layoutV加入到横向布局layoutH中
layoutH->addLayout(layoutV);
w->setLayout(layoutH);
w->show();
return a.exec();
}
效果图:
4.png当改变顺序时:
layoutH->addWidget(button00);
layoutH->addWidget(button01);
layoutH->addLayout(layoutV); //改变了插入的位置
layoutH->addWidget(button02);
layoutH->addWidget(button03);
layoutV->addWidget(button04);
layoutV->addWidget(button05);
layoutV->addWidget(button06);
效果图:
5.jpg构建子控件时不需指定parent,布局自动指定parent with QWidget::setParent()
布局中的控件是安装此布局的界面的子空间,控件只能以控件作为parent
网友评论