码迷,mamicode.com
首页 > 其他好文 > 详细

《Qt5 开发与实例(第三版)》学习笔记(九)

时间:2018-01-26 22:42:51      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:name   inf   parent   ttext   use   pre   boxlayout   mes   优化   

  1 // 3.4 基本布局(QLayout) 
  2 //dialog.h
  3 #ifndef DIALOG_H
  4 #define DIALOG_H
  5 
  6 #include <QDialog>
  7 #include <QLabel>
  8 #include <QLineEdit>
  9 #include <QComboBox>
 10 #include <QTextEdit>
 11 #include <QGridLayout>
 12 class Dialog : public QDialog
 13 {
 14     Q_OBJECT
 15 
 16 public:
 17     Dialog(QWidget *parent = 0);
 18     ~Dialog();
 19 private:
 20     //left
 21     QLabel *UserNameLabel;
 22     QLabel *NameLabel;
 23     QLabel *SexLabel;
 24     QLabel *DepartmentLabel;
 25     QLabel *AgeLabel;
 26     QLabel *OtherLabel;
 27     QLineEdit *UserNameLineEdit;
 28     QLineEdit *NameLineEdit;
 29     QComboBox *SexComboBox;
 30     QTextEdit *DepartmentTextEdit;
 31     QLineEdit *AgeLineEdit;
 32     QGridLayout *LeftLayout;
 33     //right
 34     QLabel *HeadLabel;
 35     QLabel *HeadIconLabel;
 36     QPushButton *UpdateHeadBtn;
 37     QHBoxLayout *TopRightLayout;
 38     QLabel *IntroductionLabe;
 39     QTextEdit *IntroductionTextEdit;
 40     QVBoxLayout *RightLayout;
 41     //bottom
 42     QPushButton *OkBtn;
 43     QPushButton *CancelBtn;
 44     QHBoxLayout *ButtomLayout;
 45 
 46 
 47 };
 48 
 49 #endif // DIALOG_H
 50 //dialog.cpp
 51 #include "dialog.h"
 52 #include <QLabel>
 53 #include <QLineEdit>
 54 #include <QComboBox>
 55 #include <QPushButton>
 56 #include <QFrame>
 57 #include <QGridLayout>
 58 #include <QPixmap>
 59 #include <QHBoxLayout>
 60 Dialog::Dialog(QWidget *parent)
 61     : QDialog(parent)
 62 {
 63     setWindowTitle(tr("UserInfo"));
 64     //left
 65     UserNameLabel = new QLabel(tr("User name:"));
 66     UserNameLineEdit =new QLineEdit;
 67     NameLabel =new QLabel(tr("Name:"));
 68     NameLineEdit =new QLineEdit;
 69     SexLabel =new QLabel(tr("Sex:"));
 70     SexComboBox =new QComboBox;
 71     SexComboBox->addItem(tr("Female"));
 72     SexComboBox->addItem(tr("Male"));
 73     DepartmentLabel =new QLabel(tr("Department:"));
 74     DepartmentTextEdit =new QTextEdit;
 75     AgeLabel =new QLabel(tr("Age:"));
 76     AgeLineEdit =new QLineEdit;
 77     OtherLabel =new QLabel(tr("Others:"));
 78     OtherLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);//设置控件的风格
 79     LeftLayout = new QGridLayout();
 80     LeftLayout->addWidget(UserNameLabel,0,0);//行 列
 81     LeftLayout->addWidget(UserNameLineEdit,0,1);
 82     LeftLayout->addWidget(NameLabel,1,0);
 83     LeftLayout->addWidget(NameLineEdit,1,1);
 84     LeftLayout->addWidget(SexLabel,2,0);
 85     LeftLayout->addWidget(SexComboBox,2,1);
 86     LeftLayout->addWidget(DepartmentLabel,3,0);
 87     LeftLayout->addWidget(DepartmentTextEdit,3,1);
 88     LeftLayout->addWidget(AgeLabel,4,0);
 89     LeftLayout->addWidget(AgeLineEdit,4,1);
 90     LeftLayout->addWidget(OtherLabel,5,0,1,2);
 91     LeftLayout->setColumnStretch(0,1);//设置两列占用空间的比例
 92     LeftLayout->setColumnStretch(1,3);
 93     //right
 94     HeadLabel=new QLabel(tr("Photo:"));
 95     HeadIconLabel=new QLabel;
 96     QPixmap icon("111.jpg");
 97     HeadIconLabel->setPixmap(icon);
 98     HeadIconLabel->resize(icon.width(),icon.height());
 99     UpdateHeadBtn= new QPushButton(tr("Update"));
100 
101     TopRightLayout =new QHBoxLayout();
102     TopRightLayout->setSpacing(20);//设置控件之间的间距为20
103     TopRightLayout->addWidget(HeadLabel);
104     TopRightLayout->addWidget(HeadIconLabel);
105     TopRightLayout->addWidget(UpdateHeadBtn);
106     IntroductionLabe =new QLabel(tr("Personal introduce:"));
107     IntroductionTextEdit = new QTextEdit;
108 
109     RightLayout=new QVBoxLayout();
110     RightLayout->setMargin(10);
111     RightLayout->addLayout(TopRightLayout);
112     RightLayout->addWidget(IntroductionLabe);
113     RightLayout->addWidget(IntroductionTextEdit);
114     //buttom
115     OkBtn =new QPushButton(tr("OK"));
116     CancelBtn =new QPushButton(tr("Cancel"));
117     ButtomLayout = new QHBoxLayout();
118     ButtomLayout->addStretch();//在按钮之间插入一个占位符,保证按钮大小不发生改变
119     ButtomLayout->addWidget(OkBtn);
120     ButtomLayout->addWidget(CancelBtn);
121 
122 
123     QGridLayout *mainLayout =new QGridLayout(this);//实现主布局,指定父窗口this
124     mainLayout->setMargin(15);
125     mainLayout->setSpacing(10);
126     mainLayout->addLayout(LeftLayout,0,0);
127     mainLayout->addLayout(RightLayout,0,1);
128     mainLayout->addLayout(ButtomLayout,1,0,1,2);
129     mainLayout->setSizeConstraint(QLayout::SetFixedSize);//设定最优化显示,且用户无法改变大小
130 
131 }
132 
133 Dialog::~Dialog()
134 {
135 
136 }

 

《Qt5 开发与实例(第三版)》学习笔记(九)

标签:name   inf   parent   ttext   use   pre   boxlayout   mes   优化   

原文地址:https://www.cnblogs.com/justsong/p/8361209.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!