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

Posted justsong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《Qt5 开发与实例(第三版)》学习笔记相关的知识,希望对你有一定的参考价值。

  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 开发与实例(第三版)》学习笔记的主要内容,如果未能解决你的问题,请参考以下文章

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

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

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

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

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

python cookbook第三版学习笔记十二:类和对象创建新的类或实例属性