使用横向与竖向、网格三种布局嵌套使用后可以组合出很复杂的界面。
这里向大家推荐这篇博客
http://www.cnblogs.com/Bonker/p/3454956.html
我这里使用布局做了一个对话框界面 作为练习
代码如下
qt5
QT creater创建一个Widgets application
不过没有使用该工程的UI
修改main.cpp
//#include "mainwindow.h" #include <QApplication> #include "QTestDialog.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); TestDialog* tdialog = new TestDialog; tdialog->show(); return a.exec(); }
添加
QTestDialog.h
QTestDialog.cpp
#ifndef QTESTDIALOG_H #define QTESTDIALOG_H #include <QDialog> #include <QTextEdit> #include <QPushButton> #include <QLayout> class TestDialog:public QDialog { Q_OBJECT public: TestDialog(QWidget *parent = 0); private: QTextEdit* textEdit_1; QTextEdit* textEdit_2; QTextEdit* textEdit_3; QPushButton* pushButton_1; QPushButton* pushButton_2; QPushButton* pushButton_3; QPushButton* pushButton_4; QPushButton* pushButton_5; }; #endif // QTESTDIALOG_H
#include <QApplication> #include "qtestdialog.h" TestDialog::TestDialog(QWidget *parent) : QDialog(parent) { pushButton_1 = new QPushButton(tr("字体")); pushButton_2 = new QPushButton(tr("大小")); pushButton_3 = new QPushButton(tr("消息记录")); QHBoxLayout* toolLayout = new QHBoxLayout; toolLayout->addWidget(pushButton_1); toolLayout->addWidget(pushButton_2); toolLayout->addStretch(); toolLayout->addWidget(pushButton_3); pushButton_4 = new QPushButton(tr("关闭")); pushButton_5 = new QPushButton(tr("发送")); QHBoxLayout* buttomLayout = new QHBoxLayout; buttomLayout->addStretch(); buttomLayout->addWidget(pushButton_4); buttomLayout->addWidget(pushButton_5); textEdit_1 = new QTextEdit; textEdit_2 = new QTextEdit; textEdit_2->setMaximumHeight(90); QVBoxLayout* leftlayout = new QVBoxLayout; leftlayout->addWidget(textEdit_1); leftlayout->addLayout(toolLayout); leftlayout->addWidget(textEdit_2); leftlayout->addLayout(buttomLayout); textEdit_3 = new QTextEdit; textEdit_3->setMaximumWidth(100); QVBoxLayout* rightlayout = new QVBoxLayout; rightlayout->addWidget(textEdit_3); QHBoxLayout* toplayout = new QHBoxLayout; toplayout->addLayout(leftlayout); toplayout->addLayout(rightlayout); QHBoxLayout *mainLayout = new QHBoxLayout; mainLayout->addLayout(toplayout); setLayout(mainLayout); }
最后效果图