Qt之布局管理——(1)基本布局管理

来源:互联网 发布:清华大数据联合会 编辑:程序博客网 时间:2024/05/14 19:06

Qt提供的布局类以及他们之间的继承关系(如下图):

clip_image002

常用到的布局类有:QHBoxLayout、QVBoxLayout、QGridLayout三种,分别是水平排列布局、垂直排列布局、表格排列布局。

常用的方法有addWidget()和addLayout()。addWidget()用于在布局中插入控件,addLayout()用于在布局中插入子布局。


在布局管理中还常用到setMargin()用于设定边距,setSpacing()用于设定控件间距。

setColumnStretch()用于设置列的占空比。



示例:实现如下图的布局

image

basiclayout.h

  1. #ifndef BASICLAYOUT_H  
  2. #define BASICLAYOUT_H  
  3.   
  4. #include <QtGui>  
  5.   
  6.   
  7.   
  8. class BasicLayout : public QDialog  
  9. {  
  10.     Q_OBJECT  
  11.   
  12. public:  
  13.     BasicLayout(QWidget *parent = 0, Qt::WFlags flags = 0);  
  14.     ~BasicLayout();  
  15.   
  16. private:  
  17.     QLabel *labUser;  
  18.     QLabel *labName;  
  19.     QLabel *labSex;  
  20.     QLabel *labDepartment;  
  21.     QLabel *labAge;  
  22.     QLabel *labRemark;  
  23.     QLineEdit *edtUser;  
  24.     QLineEdit *edtName;  
  25.     QComboBox *cbbSex;  
  26.     QTextEdit *edtDepartment;  
  27.     QLineEdit *edtAge;  
  28.   
  29.     QLabel *labHead;  
  30.     QLabel *labIcon;  
  31.     QLabel *labIndividual;  
  32.     QPushButton *btnChange;  
  33.     QTextEdit *edtIndividual;  
  34.   
  35.     QPushButton *btnOk;  
  36.     QPushButton *btnCancel;  
  37. };  
  38.   
  39. #endif // BASICLAYOUT_H  

basiclayout.cpp

  1. #include "basiclayout.h"  
  2.   
  3. BasicLayout::BasicLayout(QWidget *parent, Qt::WFlags flags)  
  4.     : QDialog(parent, flags)  
  5. {  
  6.     setWindowTitle(tr("User Infomation"));  
  7.       
  8.     //Left Loyout:  
  9.     labUser = new QLabel(tr("User Name:"));  
  10.     labName = new QLabel(tr("Name;"));  
  11.     labSex = new QLabel(tr("Sex:"));  
  12.     labDepartment = new QLabel(tr("Department:"));  
  13.     labAge = new QLabel(tr("Age:"));  
  14.     labRemark = new QLabel(tr("Remark:"));  
  15.     labRemark->setFrameStyle(QFrame::Panel|QFrame::Sunken);  
  16.     edtUser = new QLineEdit;  
  17.     edtName = new QLineEdit;  
  18.     cbbSex = new QComboBox;  
  19.     cbbSex->insertItem(0,tr("Female"));  
  20.     cbbSex->insertItem(1,tr("Male"));  
  21.     edtDepartment = new QTextEdit;  
  22.     edtAge = new QLineEdit;  
  23.   
  24.     QGridLayout *leftLayou = new QGridLayout;  
  25.     int col_Lab = 0;  
  26.     int col_Content = 1;  
  27.     leftLayou->addWidget(labUser,0,col_Lab);  
  28.     leftLayou->addWidget(edtUser,0,col_Content);  
  29.     leftLayou->addWidget(labName,1,col_Lab);  
  30.     leftLayou->addWidget(edtName,1,col_Content);  
  31.     leftLayou->addWidget(labSex,2,col_Lab);  
  32.     leftLayou->addWidget(cbbSex,2,col_Content);  
  33.     leftLayou->addWidget(labDepartment,3,col_Lab);  
  34.     leftLayou->addWidget(edtDepartment,3,col_Content);  
  35.     leftLayou->addWidget(labAge,4,col_Lab);  
  36.     leftLayou->addWidget(edtAge,4,col_Content);  
  37.     leftLayou->addWidget(labRemark,5,col_Lab,1,2);  
  38.     leftLayou->setColumnStretch(0,1);    //设置两列分别占有空间的比例  
  39.     leftLayou->setColumnStretch(1,3);  
  40.   
  41.     //Right Layout:  
  42.     labHead = new QLabel(tr("Head:"));  
  43.     labIcon = new QLabel;  
  44.     QPixmap m_icon("head.gif");  
  45.     labIcon->resize(m_icon.width(),m_icon.height());  
  46.     labIcon->setPixmap(m_icon);  
  47.     btnChange = new QPushButton(tr("Change"));  
  48.     QHBoxLayout *headLayout = new QHBoxLayout;  
  49.     headLayout->addWidget(labHead);  
  50.     headLayout->addWidget(labIcon);  
  51.     headLayout->addWidget(btnChange);  
  52.     headLayout->setSpacing(20);  //控件间距为20像素  
  53.     labIndividual = new QLabel(tr("Individual:"));  
  54.     edtIndividual = new QTextEdit;  
  55.     QVBoxLayout *rightLayout = new QVBoxLayout;  
  56.     rightLayout->addLayout(headLayout);  
  57.     rightLayout->addWidget(labIndividual);  
  58.     rightLayout->addWidget(edtIndividual);  
  59.     rightLayout->setMargin(10);  
  60.   
  61.     //Bottom Layout:  
  62.     btnOk = new QPushButton(tr("Ok"));  
  63.     btnCancel = new QPushButton(tr("Cancel"));  
  64.     QHBoxLayout *bottomLayout = new QHBoxLayout;  
  65.     bottomLayout->addStretch();  //添加一个占位符  
  66.     bottomLayout->addWidget(btnOk);  
  67.     bottomLayout->addWidget(btnCancel);  
  68.     bottomLayout->setSpacing(10);  
  69.   
  70.     //Main Layout:  
  71.     QGridLayout *mainLayout = new QGridLayout(this);  
  72.     mainLayout->addLayout(leftLayou,0,0);  
  73.     mainLayout->addLayout(rightLayout,0,1);  
  74.     mainLayout->addLayout(bottomLayout,1,0,1,2);  
  75.     mainLayout->setMargin(15);  
  76.     mainLayout->setSpacing(10);  
  77.     mainLayout->setSizeConstraint(QLayout::SetFixedSize);    //设置对话框大小固定,不允许用户改变  
  78.   
  79. }  
  80.   
  81. BasicLayout::~BasicLayout()  
  82. {  
  83.   
  84. }  

setFrameStyle()是QFrame的方法,参数以或的方式设定控件的风格,参数1(QFrame::Shape)用于设定控件的形状,参数2(QFrame::Shadow)用于设定控件俺的阴影。

形状有:NoFrame、Panel、Box、HLine、VLine、WinPanel 6种;阴影有:Plain、Raised、Sunken三种。

1 0
原创粉丝点击