qt--QVBoxLayout

 1     QWidget *window = new QWidget();
 2     //创建按键,暂无配置slot
 3     QPushButton *button1 = new QPushButton(tr("one"));
 4     QPushButton *button2 = new QPushButton(tr("two"));
 5     QPushButton *button3 = new QPushButton(tr("three"));
 6     QPushButton *button4 = new QPushButton(tr("four"));
 7     QPushButton *button5 = new QPushButton(tr("five"));
 8     //垂直布局
 9     QVBoxLayout *layout = new QVBoxLayout;
10     layout->addWidget(button1);
11     layout->addWidget(button2);
12     layout->addWidget(button3);
13     layout->addWidget(button4);
14     layout->addWidget(button5);
15 
16     window->setLayout(layout);
17     window->show();

//QVBoxLayout(垂直布局) QHBoxLayout(水平布局) 继承 QBoxLayout

 修改布局

 1 #include "widget.h"
 2 //#include "addDialog.h"
 3 #include <QtGui>
 4 
 5 Widget::Widget(QWidget *parent)
 6     : QWidget(parent)
 7 {
 8     createbigBox();
 9     createsmallBox();
10 
11     QWidget *window = new QWidget();
12     //创建按键,暂无配置slot
13 
14 
15 
16     //垂直布局
17     QVBoxLayout *mainLayout = new QVBoxLayout;
18     mainLayout->addWidget(bigBox);
19     mainLayout->addWidget(smallBox);
20 
21     window->setLayout(mainLayout);
22     setWindowTitle(tr("layout-Demo"));
23     window->show();
24 
25 }
26 
27 Widget::~Widget()
28 {
29     
30 }
31 
32 void Widget::createbigBox()
33 {
34     bigBox = new QGroupBox(tr("bigGroupBox"));
35 
36     QPushButton *button1 = new QPushButton(tr("one"));
37     //connect(button1,SIGNAL(clicked()),SLOT(open_One()));
38     QPushButton *button2 = new QPushButton(tr("two"));
39     QPushButton *button3 = new QPushButton(tr("three"));
40     //水平布局
41     QHBoxLayout *horizeLayout = new QHBoxLayout;
42     horizeLayout->addWidget(button1);
43     horizeLayout->addWidget(button2);
44     horizeLayout->addWidget(button3);
45 
46     bigBox->setLayout(horizeLayout);
47 }
48 
49 void Widget::createsmallBox()
50 {
51     smallBox = new QGroupBox(tr("smallGroupBox"));
52 
53     QPushButton *button4 = new QPushButton(tr("four"));
54     QPushButton *button5 = new QPushButton(tr("five"));
55 
56     QHBoxLayout *toLayout = new QHBoxLayout;
57     toLayout->addWidget(button4);
58     toLayout->addWidget(button5);
59 
60     smallBox->setLayout(toLayout);
61 }
62 /*
63 //槽函数
64 void Widget::open_one()
65 {
66     Widget.widget;
67 }
68 */
原文地址:https://www.cnblogs.com/Blue-Moniter4/p/9528702.html