*****************************************************mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
******************************************************mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QWidget>
#include <QSpacerItem>
#include <QGroupBox>
#include <QTextEdit>
#include <QSpinBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
this->resize(500,200);
QPushButton *pb1 = new QPushButton("Commit");
QPushButton *pb2 = new QPushButton("Commit");
//QGroupBox 给hl1中添加
QGroupBox *gb1 = new QGroupBox("QGroupBox");
QHBoxLayout *thl1 = new QHBoxLayout();
thl1->addWidget(new QLabel("Title1"));
thl1->addWidget(new QLineEdit());
QHBoxLayout *thl2 = new QHBoxLayout();
thl2->addWidget(new QLabel("Title2"));
thl2->addWidget(new QLineEdit());
QHBoxLayout *thl3 = new QHBoxLayout();
thl3->addWidget(new QLabel("Title3"));
thl3->addWidget(new QLineEdit());
QHBoxLayout *thl4 = new QHBoxLayout();
thl4->addWidget(new QSpinBox());
thl4->addWidget(new QLineEdit());
QVBoxLayout *tvl = new QVBoxLayout();
tvl->addLayout(thl1);
tvl->addLayout(thl2);
tvl->addLayout(thl3);
tvl->addLayout(thl4);
gb1->setLayout(tvl);
QVBoxLayout *hl1 = new QVBoxLayout(this);
hl1->addWidget(gb1);
hl1->addWidget(pb1);
QVBoxLayout *hl2 = new QVBoxLayout(this);
hl2->addWidget(new QTextEdit("Test textedit..."));
hl2->addWidget(pb2);
//将两个垂直的布局键入到一个水平布局中
QHBoxLayout *hl3 = new QHBoxLayout(this); //为一个水平布局
hl3->addLayout(hl1);
QSpacerItem *spaceItem = new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);
hl3->addSpacerItem(spaceItem);
hl3->addLayout(hl2);
QVBoxLayout *mainVbox = new QVBoxLayout(this);//最外层的vbox
QHBoxLayout *bottomHbox = new QHBoxLayout();//最外层vbox 最下面的hbox
QLabel *qlabel_1 = new QLabel("BottomLabel");
QLineEdit *lineEdit_1 = new QLineEdit();
bottomHbox->addWidget(qlabel_1);
bottomHbox->addWidget(lineEdit_1);
//将两个水平布局加入最外层的垂直布局
mainVbox->addLayout(hl3);
mainVbox->addLayout(bottomHbox);
QWidget *widget = new QWidget();
widget->setLayout(mainVbox);
this->setCentralWidget(widget);
}
MainWindow::~MainWindow()
{
//delete ui;
}