Qt提供的布局類以及他們之間的繼承關系(如下圖):
常用到的布局類有:QHBoxLayout、QVBoxLayout、QGridLayout三種,分別是水平排列布局、垂直排列布局、表格排列布局。
常用的方法有addWidget()和addLayout()。addWidget()用於在布局中插入控件,addLayout()用於在布局中插入子布局。
在布局管理中還常用到setMargin()用於設定邊距,setSpacing()用於設定控件間距。
setColumnStretch()用於設置列的占空比。
示例:實現如下圖的布局
basiclayout.h
#ifndef BASICLAYOUT_H #define BASICLAYOUT_H #include <QtGui> class BasicLayout : public QDialog { Q_OBJECT public: BasicLayout(QWidget *parent = 0, Qt::WFlags flags = 0); ~BasicLayout(); private: QLabel *labUser; QLabel *labName; QLabel *labSex; QLabel *labDepartment; QLabel *labAge; QLabel *labRemark; QLineEdit *edtUser; QLineEdit *edtName; QComboBox *cbbSex; QTextEdit *edtDepartment; QLineEdit *edtAge; QLabel *labHead; QLabel *labIcon; QLabel *labIndividual; QPushButton *btnChange; QTextEdit *edtIndividual; QPushButton *btnOk; QPushButton *btnCancel; }; #endif // BASICLAYOUT_H
basiclayout.cpp
#include "basiclayout.h" BasicLayout::BasicLayout(QWidget *parent, Qt::WFlags flags) : QDialog(parent, flags) { setWindowTitle(tr("User Infomation")); //Left Loyout: labUser = new QLabel(tr("User Name:")); labName = new QLabel(tr("Name;")); labSex = new QLabel(tr("Sex:")); labDepartment = new QLabel(tr("Department:")); labAge = new QLabel(tr("Age:")); labRemark = new QLabel(tr("Remark:")); labRemark->setFrameStyle(QFrame::Panel|QFrame::Sunken); edtUser = new QLineEdit; edtName = new QLineEdit; cbbSex = new QComboBox; cbbSex->insertItem(0,tr("Female")); cbbSex->insertItem(1,tr("Male")); edtDepartment = new QTextEdit; edtAge = new QLineEdit; QGridLayout *leftLayou = new QGridLayout; int col_Lab = 0; int col_Content = 1; leftLayou->addWidget(labUser,0,col_Lab); leftLayou->addWidget(edtUser,0,col_Content); leftLayou->addWidget(labName,1,col_Lab); leftLayou->addWidget(edtName,1,col_Content); leftLayou->addWidget(labSex,2,col_Lab); leftLayou->addWidget(cbbSex,2,col_Content); leftLayou->addWidget(labDepartment,3,col_Lab); leftLayou->addWidget(edtDepartment,3,col_Content); leftLayou->addWidget(labAge,4,col_Lab); leftLayou->addWidget(edtAge,4,col_Content); leftLayou->addWidget(labRemark,5,col_Lab,1,2); leftLayou->setColumnStretch(0,1); //設置兩列分別占有空間的比例 leftLayou->setColumnStretch(1,3); //Right Layout: labHead = new QLabel(tr("Head:")); labIcon = new QLabel; QPixmap m_icon("head.gif"); labIcon->resize(m_icon.width(),m_icon.height()); labIcon->setPixmap(m_icon); btnChange = new QPushButton(tr("Change")); QHBoxLayout *headLayout = new QHBoxLayout; headLayout->addWidget(labHead); headLayout->addWidget(labIcon); headLayout->addWidget(btnChange); headLayout->setSpacing(20); //控件間距為20像素 labIndividual = new QLabel(tr("Individual:")); edtIndividual = new QTextEdit; QVBoxLayout *rightLayout = new QVBoxLayout; rightLayout->addLayout(headLayout); rightLayout->addWidget(labIndividual); rightLayout->addWidget(edtIndividual); rightLayout->setMargin(10); //Bottom Layout: btnOk = new QPushButton(tr("Ok")); btnCancel = new QPushButton(tr("Cancel")); QHBoxLayout *bottomLayout = new QHBoxLayout; bottomLayout->addStretch(); //添加一個占位符 bottomLayout->addWidget(btnOk); bottomLayout->addWidget(btnCancel); bottomLayout->setSpacing(10); //Main Layout: QGridLayout *mainLayout = new QGridLayout(this); mainLayout->addLayout(leftLayou,0,0); mainLayout->addLayout(rightLayout,0,1); mainLayout->addLayout(bottomLayout,1,0,1,2); mainLayout->setMargin(15); mainLayout->setSpacing(10); mainLayout->setSizeConstraint(QLayout::SetFixedSize); //設置對話框大小固定,不允許用戶改變 } BasicLayout::~BasicLayout() { }
setFrameStyle()是QFrame的方法,參數以或的方式設定控件的風格,參數1(QFrame::Shape)用於設定控件的形狀,參數2(QFrame::Shadow)用於設定控件俺的陰影。
形狀有:NoFrame、Panel、Box、HLine、VLine、WinPanel 6種;陰影有:Plain、Raised、Sunken三種。