Qt学习——布局管理器QLayout类 .

 

常用的布局管理有QVBoxLayout,QHBoxLayout,QGridLayout。

下面是综合应用:

Qt学习——布局管理器QLayout类 ._第1张图片


新建Qt Gui程序,基类为Dialog

头文件:

view plain copy to clipboard print ?
  1. #ifndef DIALOG_H   
  2. #define DIALOG_H   
  3.   
  4. #include <QtGui/QDialog>   
  5. #include <QLabel>   
  6. #include <QLineEdit>   
  7. #include <QTextEdit>   
  8. #include <QComboBox>   
  9. #include <QGridLayout>   
  10. #include <QPushButton>   
  11. #include <QHBoxLayout>   
  12. #include <QVBoxLayout>   
  13. class Dialog : public QDialog  
  14. {  
  15.     Q_OBJECT  
  16.   
  17. public:  
  18.     Dialog(QWidget *parent = 0);  
  19.     ~Dialog();  
  20.   
  21. private:  
  22.     //左侧的控件   
  23.     QLabel *UserLabel;  
  24.     QLabel *NameLabel;  
  25.     QLabel *SexLabel;  
  26.     QLabel *DepartLabel;  
  27.     QLabel *AgeLabel;  
  28.     QLabel *OtherLabel;  
  29.     QLineEdit *UserLineEdit;  
  30.     QLineEdit *NameLineEdit;  
  31.     QComboBox *SexComboBox;  
  32.     QTextEdit *DepartTextEdit;  
  33.     QLineEdit *AgeLineEdit;  
  34.     QGridLayout *LeftGridLayout;  
  35.     //右上角   
  36.     QLabel *HeadLabel;  
  37.     QLabel *HeadIconLabel;  
  38.     QPushButton *UpdateHeadBtn;  
  39.     QHBoxLayout *RightTopHBLayout;  
  40.     //右下角   
  41.     QLabel *IntroLabel;  
  42.     QTextEdit *IntroTextEdit;  
  43.     QVBoxLayout *RightVBLayout;  
  44.     //底部   
  45.     QPushButton *OkBtn;  
  46.     QPushButton *CancelBtn;  
  47.     QHBoxLayout *ButtomHBLayout;  
  48. };  
  49.   
  50. #endif // DIALOG_H  
#ifndef DIALOG_H #define DIALOG_H #include <QtGui/QDialog> #include <QLabel> #include <QLineEdit> #include <QTextEdit> #include <QComboBox> #include <QGridLayout> #include <QPushButton> #include <QHBoxLayout> #include <QVBoxLayout> class Dialog : public QDialog { Q_OBJECT public: Dialog(QWidget *parent = 0); ~Dialog(); private: //左侧的控件 QLabel *UserLabel; QLabel *NameLabel; QLabel *SexLabel; QLabel *DepartLabel; QLabel *AgeLabel; QLabel *OtherLabel; QLineEdit *UserLineEdit; QLineEdit *NameLineEdit; QComboBox *SexComboBox; QTextEdit *DepartTextEdit; QLineEdit *AgeLineEdit; QGridLayout *LeftGridLayout; //右上角 QLabel *HeadLabel; QLabel *HeadIconLabel; QPushButton *UpdateHeadBtn; QHBoxLayout *RightTopHBLayout; //右下角 QLabel *IntroLabel; QTextEdit *IntroTextEdit; QVBoxLayout *RightVBLayout; //底部 QPushButton *OkBtn; QPushButton *CancelBtn; QHBoxLayout *ButtomHBLayout; }; #endif // DIALOG_H
源文件:

view plain copy to clipboard print ?
  1. #include "dialog.h"   
  2.   
  3. Dialog::Dialog(QWidget *parent)  
  4.     : QDialog(parent)  
  5. {  
  6.     //××××××××××××初始化×××××××××××××××   
  7.     setWindowTitle(tr("UserInfo"));  
  8.     UserLabel=new QLabel(tr("用户名:"));  
  9.     UserLineEdit=new QLineEdit;  
  10.     NameLabel=new QLabel(tr("姓名:"));  
  11.     NameLineEdit=new QLineEdit;  
  12.     SexLabel=new QLabel(tr("性别:"));  
  13.     SexComboBox=new QComboBox;  
  14.     SexComboBox->addItem(tr("男"));  
  15.     SexComboBox->addItem(tr("女"));  
  16.     DepartLabel=new QLabel(tr("部门"));  
  17.     DepartTextEdit=new QTextEdit;  
  18.     AgeLabel=new QLabel(tr("年龄"));  
  19.     AgeLineEdit=new QLineEdit;  
  20.     OtherLabel=new QLabel(tr("备注"));  
  21.     OtherLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);  
  22.     //××××××××××××添加左侧××××××××××××××   
  23.     LeftGridLayout=new QGridLayout();  
  24.     LeftGridLayout->addWidget(UserLabel,0,0);  
  25.     LeftGridLayout->addWidget(UserLineEdit,0,1);  
  26.   
  27.     LeftGridLayout->addWidget(NameLabel,1,0);  
  28.     LeftGridLayout->addWidget(NameLineEdit,1,1);  
  29.   
  30.     LeftGridLayout->addWidget(SexLabel,2,0);  
  31.     LeftGridLayout->addWidget(SexComboBox,2,1);  
  32.   
  33.     LeftGridLayout->addWidget(DepartLabel,3,0);  
  34.     LeftGridLayout->addWidget(DepartTextEdit,3,1);  
  35.   
  36.     LeftGridLayout->addWidget(AgeLabel,4,0);  
  37.     LeftGridLayout->addWidget(AgeLineEdit,4,1);  
  38.   
  39.     LeftGridLayout->addWidget(OtherLabel,5,0,1,2);  
  40.   
  41.     LeftGridLayout->setColumnStretch(0,1);  
  42.     LeftGridLayout->setColumnStretch(1,3);  
  43.     //×××××××××××××添加右上角×××××××××××××××   
  44.     HeadLabel=new QLabel(tr("头像:"));  
  45.     HeadIconLabel=new QLabel;  
  46.     QPixmap icon("1.bmp");  
  47.     HeadIconLabel->setPixmap(icon);  
  48.     HeadIconLabel->resize(icon.width(),icon.height());  
  49.     UpdateHeadBtn=new QPushButton(tr("更新"));  
  50.   
  51.     RightTopHBLayout=new QHBoxLayout;  
  52.     RightTopHBLayout->setSpacing(20);  
  53.     RightTopHBLayout->addWidget(HeadLabel);  
  54.     RightTopHBLayout->addWidget(HeadIconLabel);  
  55.     RightTopHBLayout->addWidget(UpdateHeadBtn);  
  56.     //×××××××××××××添加右下角×××××××××××××××   
  57.     IntroLabel=new QLabel(tr("个人说明:"));  
  58.     IntroTextEdit=new QTextEdit;  
  59.   
  60.     RightVBLayout=new QVBoxLayout();  
  61.     RightVBLayout->setMargin(10);  
  62.     RightVBLayout->addLayout(RightTopHBLayout);  
  63.     RightVBLayout->addWidget(IntroLabel);  
  64.     RightVBLayout->addWidget(IntroTextEdit);  
  65.     //×××××××××××××添加底部×××××××××××××××   
  66.     OkBtn=new QPushButton(tr("确定"));  
  67.     CancelBtn=new QPushButton(tr("取消"));  
  68.   
  69.     ButtomHBLayout=new QHBoxLayout();  
  70.     ButtomHBLayout->addStretch();  
  71.     ButtomHBLayout->addWidget(OkBtn);  
  72.     ButtomHBLayout->addWidget(CancelBtn);  
  73.     //×××××××××××××设置主窗体×××××××××××××××   
  74.     QGridLayout *mainLayout=new QGridLayout(this);  
  75.     mainLayout->setMargin(15);  
  76.     mainLayout->setSpacing(10);  
  77.     mainLayout->addLayout(LeftGridLayout,0,0);  
  78.     mainLayout->addLayout(RightVBLayout,0,1);  
  79.     mainLayout->addLayout(ButtomHBLayout,1,0,1,2);  
  80.     mainLayout->setSizeConstraint(QLayout::SetFixedSize);  
  81.   
  82.     connect(OkBtn,SIGNAL(clicked()),this,SLOT(accept()));  
  83.     connect(CancelBtn,SIGNAL(clicked()),this,SLOT(reject()));  
  84. }  
  85.   
  86. Dialog::~Dialog()  
  87. {  
  88.   
  89. }  

你可能感兴趣的:(Qt学习——布局管理器QLayout类 .)