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

Posted justsong

tags:

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

  1 // 3.5 『综合实例』 修改用户资料
  2 //main.cpp
  3 #include "content.h"
  4 #include <QApplication>
  5 #include <QTextCodec>
  6 #include <QSplitter>
  7 #include <QListWidget>
  8 int main(int argc, char *argv[])
  9 {
 10     QApplication a(argc, argv);
 11     QFont font("AR PL KaitiM GB",12);
 12     a.setFont(font);
 13     QSplitter *splitterMain =new QSplitter(Qt::Horizontal);//新建水平分割窗口
 14     splitterMain->setOpaqueResize(true);
 15     QListWidget *list =new QListWidget(splitterMain);
 16     list->insertItem(0,QObject::tr("Basic information"));
 17     list->insertItem(1,QObject::tr("Contact information"));
 18     list->insertItem(2,QObject::tr("Details"));
 19     Content *content =new Content(splitterMain);
 20     QObject::connect(list,SIGNAL(currentRowChanged(int)),content->stack,SLOT(setCurrentIndex(int)));
 21     splitterMain->setWindowTitle(QObject::tr("Modify user information"));
 22     splitterMain->setMinimumSize(splitterMain->minimumSize());
 23     splitterMain->setMaximumSize(splitterMain->maximumSize());
 24     splitterMain->show();
 25     //Content w;
 26     //w.show();
 27 
 28     return a.exec();
 29 }
 30 //content.h
 31 #ifndef CONTENT_H
 32 #define CONTENT_H
 33 
 34 #include <QStackedWidget>
 35 #include <QPushButton>
 36 #include <QFrame>
 37 #include "baseinfo.h"
 38 //#include "contact.h"
 39 //#include "detail.h"
 40 
 41 class Content : public QFrame
 42 {
 43     Q_OBJECT
 44 
 45 public:
 46     Content(QWidget *parent = 0);
 47     ~Content();
 48     QStackedWidget *stack;
 49     QPushButton *AmeddBtn;
 50     QPushButton *CloseBtn;
 51     BaseInfo *baseInfo;
 52     //Contact *contact;
 53     //Detail *detail;
 54 };
 55 
 56 #endif // CONTENT_H
 57 
 58 //content.cpp
 59 #include "content.h"
 60 #include <QHBoxLayout>
 61 #include <QVBoxLayout>
 62 Content::Content(QWidget *parent)
 63     : QFrame(parent)
 64 {
 65     stack =new QStackedWidget(this);//堆叠
 66     stack->setFrameStyle(QFrame::Panel|QFrame::Raised);
 67 
 68     baseInfo =new BaseInfo();
 69     //detail =new Detail();
 70     //contact =new Contact();
 71     stack->addWidget(baseInfo);
 72    // stack->addWidget(contact);
 73     //stack->addWidget(detail);
 74 
 75     AmeddBtn =new QPushButton(tr("Change"));
 76     CloseBtn =new QPushButton(tr("Close"));
 77     QHBoxLayout *BtnLayout=new QHBoxLayout;
 78     BtnLayout->addStretch(1);
 79     BtnLayout->addWidget(AmeddBtn);
 80     BtnLayout->addWidget(CloseBtn);
 81 
 82     QVBoxLayout *RightLayout =new QVBoxLayout(this);
 83     RightLayout->setMargin(10);//边缘
 84     RightLayout->setSpacing(6);//间距
 85     RightLayout->addWidget(stack);
 86     RightLayout->addLayout(BtnLayout);
 87 }
 88 
 89 Content::~Content()
 90 {
 91 
 92 }
 93 //baseinfo.h
 94 #ifndef BASEINFO_H
 95 #define BASEINFO_H
 96 
 97 #include <QWidget>
 98 #include <QLabel>
 99 #include <QLineEdit>
100 #include <QComboBox>
101 #include <QTextEdit>
102 #include <QPushButton>
103 #include <QGridLayout>
104 class BaseInfo : public QWidget
105 {
106     Q_OBJECT
107 public:
108     explicit BaseInfo(QWidget *parent = nullptr);
109 
110 signals:
111 
112 public slots:
113 private:
114     //left
115     QLabel *UserNameLabel;
116     QLabel *NameLabel;
117     QLabel *SexLabel;
118     QLabel *DepartmentLabel;
119     QLabel *AgeLabel;
120     QLabel *OtherLabel;
121     QLineEdit *UserNameLineEdit;
122     QLineEdit *NameLineEdit;
123     QComboBox *SexComboBox;
124     QTextEdit *DepartmentTextEdit;
125     QLineEdit *AgeLineEdit;
126     QGridLayout *LeftLayout;
127     //right
128     QLabel *HeadLabel;
129     QLabel *HeadIconLabel;
130     QPushButton *UpdateHeadBtn;
131     QHBoxLayout *TopRightLayout;
132     QLabel *IntroductionLabel;
133     QTextEdit *IntroductionTextEdit;
134     QVBoxLayout *RightLayout;
135 };
136 
137 #endif // BASEINFO_H
138 
139 //base.cpp
140 #include "baseinfo.h"
141 
142 BaseInfo::BaseInfo(QWidget *parent) : QWidget(parent)
143 {
144     UserNameLabel = new QLabel(tr("User name:"));
145     UserNameLineEdit =new QLineEdit;
146     NameLabel =new QLabel(tr("Name:"));
147     NameLineEdit =new QLineEdit;
148     SexLabel =new QLabel(tr("Sex:"));
149     SexComboBox =new QComboBox;
150     SexComboBox->addItem(tr("Female"));
151     SexComboBox->addItem(tr("Male"));
152     DepartmentLabel =new QLabel(tr("Department:"));
153     DepartmentTextEdit =new QTextEdit;
154     AgeLabel =new QLabel(tr("Age:"));
155     AgeLineEdit =new QLineEdit;
156     OtherLabel =new QLabel(tr("Others:"));
157     OtherLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);//设置控件的风格
158     LeftLayout = new QGridLayout();
159     LeftLayout->addWidget(UserNameLabel,0,0);//行 列
160     LeftLayout->addWidget(UserNameLineEdit,0,1);
161     LeftLayout->addWidget(NameLabel,1,0);
162     LeftLayout->addWidget(NameLineEdit,1,1);
163     LeftLayout->addWidget(SexLabel,2,0);
164     LeftLayout->addWidget(SexComboBox,2,1);
165     LeftLayout->addWidget(DepartmentLabel,3,0);
166     LeftLayout->addWidget(DepartmentTextEdit,3,1);
167     LeftLayout->addWidget(AgeLabel,4,0);
168     LeftLayout->addWidget(AgeLineEdit,4,1);
169     LeftLayout->addWidget(OtherLabel,5,0,1,2);
170     LeftLayout->setColumnStretch(0,1);//设置两列占用空间的比例
171     LeftLayout->setColumnStretch(1,3);
172     //right
173     HeadLabel=new QLabel(tr("Photo:"));
174     HeadIconLabel=new QLabel;
175     QPixmap icon("111.jpg");
176     HeadIconLabel->setPixmap(icon);
177     HeadIconLabel->resize(icon.width(),icon.height());
178     UpdateHeadBtn= new QPushButton(tr("Update"));
179 
180     TopRightLayout =new QHBoxLayout();
181     TopRightLayout->setSpacing(20);//设置控件之间的间距为20
182     TopRightLayout->addWidget(HeadLabel);
183     TopRightLayout->addWidget(HeadIconLabel);
184     TopRightLayout->addWidget(UpdateHeadBtn);
185     IntroductionLabel =new QLabel(tr("Personal introduce:"));
186     IntroductionTextEdit = new QTextEdit;
187 
188     RightLayout=new QVBoxLayout();
189     RightLayout->setMargin(10);
190     RightLayout->addLayout(TopRightLayout);
191     RightLayout->addWidget(IntroductionLabel);
192     RightLayout->addWidget(IntroductionTextEdit);
193 
194     QGridLayout *mainLayout =new QGridLayout(this);
195     mainLayout->setMargin(15);
196     mainLayout->setSpacing(10);
197     mainLayout->addLayout(LeftLayout,0,0);
198     mainLayout->addLayout(RightLayout,0,1);
199     mainLayout->setSizeConstraint(QLayout::SetFixedSize);
200 }

 

以上是关于《Qt5 开发与实例(第三版)》学习笔记的主要内容,如果未能解决你的问题,请参考以下文章

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

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

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

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

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

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