项目5 (综合实例)修改用户资料
1、基本信息页面
2、联系方式页面
3、详细资料页面
最外层是一个分割窗口QSplitter,分割窗体的左侧是一个QListWidget,右侧是一个QVBoxLayout布局,次布局包括一个堆栈窗体QStackWidget和一个按钮的布局。
在此堆栈窗体QStackedWidget中包含三个窗体,每一个窗体采用基本布局方式进行布局管理。
main.cpp
#include "content.h"
#include <QApplication>
#include <QSplitter>
#include <QListWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//设置整个程序采用的字体与字号
QFont font("AR PL KaitiM GB", 12);
a.setFont (font);
//新建一个水平分割对象,作为主布局框
QSplitter *splitterMain = new QSplitter(Qt::Horizontal, 0);
//设定主分割窗口的分隔条在拖曳时是否为实时更新显示
splitterMain->setOpaqueResize (true);
QListWidget *list = new QListWidget(splitterMain);
list->insertItem (0, QObject::tr("基本信息"));
list->insertItem (1, QObject::tr("联系方式"));
list->insertItem (2, QObject::tr("详细资料"));
Content *content = new Content(splitterMain);
QObject::connect (list, SIGNAL(currentRowChanged(int)), content->stack, SLOT(setCurrentIndex(int)));
splitterMain->setWindowTitle (QObject::tr("修改用户资料"));
//设置主布局框水平分割窗口的最小尺寸
splitterMain->setMinimumSize (splitterMain->minimumSize ());
//设置主布局框水平分割窗口的最大尺寸
splitterMain->setMaximumSize (splitterMain->maximumSize ());
splitterMain->show ();
return a.exec();
}
content.h
#ifndef CONTENT_H
#define CONTENT_H
#include <QStackedWidget>
#include <QPushButton>
#include "baseinfo.h"
#include "contact.h"
#include "detail.h"
class Content : public QFrame
{
Q_OBJECT
public:
Content(QWidget *parent = 0);
~Content();
QStackedWidget *stack; //定义一个堆栈窗体指针
QPushButton *AmendBtn; //定义一个修改按钮
QPushButton *CloseBtn; //定义一个关闭按钮
BaseInfo *baseInfo; //定义一个基本信息页面指针
Contact *contact; //定义一个联系方式页面指针
Detail *detail; //定义一个详细资料页面指针
};
#endif // CONTENT_H
content.cpp
#include "content.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
Content::Content(QWidget *parent)
:QFrame(parent)
{
//创建一个QStackedWidget对象
stack = new QStackedWidget(this);
//对堆栈窗口的显示风格进行设置
stack->setFrameStyle (QFrame::Panel | QFrame::Raised);
//向stack中插入三个页面
baseInfo = new BaseInfo();
contact = new Contact();
detail = new Detail();
stack->addWidget (baseInfo);
stack->addWidget (contact);
stack->addWidget (detail);
//创建两个按钮
AmendBtn = new QPushButton(tr("修改"));
CloseBtn = new QPushButton(tr("关闭"));
QHBoxLayout *BtnLayout = new QHBoxLayout;
/*在按钮之前插入一个占位符,使两个按钮能够靠右对齐,并且当整个对话框大小发生改变时,保证按钮的大小不发生变化*/
BtnLayout->addStretch (1);
BtnLayout->addWidget (AmendBtn);
BtnLayout->addWidget (CloseBtn);
QVBoxLayout * RightLayout = new QVBoxLayout(this);
RightLayout->setMargin (15); //设定对话框(或窗体)的边距为15
RightLayout->setSpacing (10); //设定各个部件之间的间距为10
RightLayout->addWidget (stack);
RightLayout->addLayout (BtnLayout);
}
Content::~Content()
{
delete stack;
}
baseinfo.h
#ifndef BASEINFO_H
#define BASEINFO_H
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QTextEdit>
#include <QPushButton>
#include <QGridLayout>
class BaseInfo : public QWidget
{
Q_OBJECT
public:
explicit BaseInfo(QWidget *parent = 0);
private:
//左侧
QLabel *UserNameLabel; //定义一个用户名标签的指针
QLabel *NameLabel; //定义一个姓名标签的指针
QLabel *SexLabel; //定义一个性别标签的指针
QLabel *DepartmentLabel; //定义一个部门标签的指针
QLabel * AgeLabel; //定义一个年龄标签的指针
QLabel *OtherLabel; //定义一个备注标签的指针
QLineEdit *UserNameLineEdit;
QLineEdit * NameLineEdit;
QComboBox *SexComboBox;
QTextEdit *DepartmentTextEdit;
QLineEdit *AgeLineEdit;
QGridLayout *LeftLayout;
//右册上部分
QLabel *HeadLabel;
QLabel *HeadIconLabel;
QPushButton *UpdateHeadBtn;
QHBoxLayout *TopRightLayout;
//右侧下部分
QLabel *IntroductionLabel;
QTextEdit *IntroductionTextEdit;
QVBoxLayout *RightLayout;
};
#endif // BASEINFO_H
baseinfo.cpp
#include "baseinfo.h"
BaseInfo::BaseInfo(QWidget *parent) : QWidget(parent)
{
//左侧
UserNameLabel = new QLabel(tr("用户名"));
UserNameLineEdit = new QLineEdit;
NameLabel = new QLabel(tr("姓名"));
NameLineEdit = new QLineEdit;
SexLabel = new QLabel(tr("性别"));
SexComboBox = new QComboBox;
SexComboBox->addItem (tr("男"));
SexComboBox->addItem (tr("女"));
DepartmentLabel = new QLabel(tr("部门"));
DepartmentTextEdit = new QTextEdit;
AgeLabel = new QLabel(tr("年龄"));
AgeLineEdit = new QLineEdit;
OtherLabel = new QLabel(tr("备注"));
OtherLabel->setFrameStyle (QFrame::Panel | QFrame::Sunken);
//左侧布局
LeftLayout = new QGridLayout();
LeftLayout->addWidget (UserNameLabel, 0, 0);
LeftLayout->addWidget (UserNameLineEdit, 0, 1);
LeftLayout->addWidget (NameLabel, 1, 0);
LeftLayout->addWidget (NameLineEdit, 1, 1);
LeftLayout->addWidget (SexLabel, 2, 0);
LeftLayout->addWidget (SexComboBox, 2, 1);
LeftLayout->addWidget (DepartmentLabel, 3, 0);
LeftLayout->addWidget (DepartmentTextEdit, 3, 1);
LeftLayout->addWidget (AgeLabel, 4, 0);
LeftLayout->addWidget (AgeLineEdit, 4, 1);
LeftLayout->addWidget (OtherLabel, 5, 0, 1, 2);
LeftLayout->setColumnStretch (0, 1);
LeftLayout->setColumnStretch (1, 3);
//右侧上部分
HeadLabel = new QLabel(tr("头像"));
HeadIconLabel = new QLabel;
QPixmap icon("312.png");
HeadIconLabel->setPixmap (icon);
HeadIconLabel->resize (icon.width (), icon.height ());
UpdateHeadBtn = new QPushButton(tr("更新"));
TopRightLayout = new QHBoxLayout();
TopRightLayout->setSpacing (20);
TopRightLayout->addWidget (HeadLabel);
TopRightLayout->addWidget (HeadIconLabel);
TopRightLayout->addWidget (UpdateHeadBtn);
//右侧下部分
IntroductionLabel = new QLabel(tr("个人说明"));
IntroductionTextEdit = new QTextEdit;
RightLayout = new QVBoxLayout();
RightLayout->setMargin (10);
RightLayout->addLayout (TopRightLayout);
RightLayout->addWidget (IntroductionLabel);
RightLayout->addWidget (IntroductionTextEdit);
QGridLayout *mainLayout = new QGridLayout(this);
mainLayout->setMargin (15);
mainLayout->setSpacing (10);
mainLayout->addLayout (LeftLayout, 0, 0);
mainLayout->addLayout (RightLayout, 0, 1);
//设定最优化显示,并且使用户无法改变对话框的大小.所谓最优化显示,即控件都按其SizeHint()的大小显示
mainLayout->setSizeConstraint (QLayout::SetFixedSize);
}
contact.h
#ifndef CONTACT_H
#define CONTACT_H
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QCheckBox>
#include <QGridLayout>
class Contact : public QWidget
{
Q_OBJECT
public:
explicit Contact(QWidget *parent = 0);
private:
QLabel *EmailLabel; //电子邮件
QLabel *AddrLabel; //联系地址
QLabel *CodeLabel; //邮政编码
QLabel *MoviTelLabel; //移动电话
QCheckBox *MoviTelCheckBook;//接收留言复选框
QLabel *ProTelLabel; //办公电话
QLineEdit *EmailLineEdit;
QLineEdit *AddrLineEdit;
QLineEdit *CodeLineEdit;
QLineEdit *MoviTelLineEdit;
QLineEdit *ProTelLineEdit;
QGridLayout *mainLayout;
};
#endif // CONTACT_H
contact.cpp
#include "contact.h"
Contact::Contact(QWidget *parent) : QWidget(parent)
{
EmailLabel = new QLabel(tr("电子邮件:"));
EmailLineEdit = new QLineEdit;
AddrLabel = new QLabel(tr("联系地址:"));
AddrLineEdit= new QLineEdit;
CodeLabel = new QLabel(tr("邮政编码:"));
CodeLineEdit = new QLineEdit;
MoviTelLabel = new QLabel(tr("移动电话:"));
MoviTelLineEdit = new QLineEdit;
MoviTelCheckBook = new QCheckBox(tr("接收短信"));
ProTelLabel = new QLabel(tr("办公电话:"));
ProTelLineEdit = new QLineEdit;
mainLayout = new QGridLayout(this);
mainLayout->setMargin (15);
mainLayout->setSpacing (10);
mainLayout->addWidget (EmailLabel, 0, 0);
mainLayout->addWidget (EmailLineEdit, 0, 1);
mainLayout->addWidget (AddrLabel, 1, 0);
mainLayout->addWidget (AddrLineEdit, 1, 1);
mainLayout->addWidget (CodeLabel, 2, 0);
mainLayout->addWidget (CodeLineEdit, 2, 1);
mainLayout->addWidget (MoviTelLabel, 3, 0);
mainLayout->addWidget (MoviTelLineEdit, 3, 1);
mainLayout->addWidget (MoviTelCheckBook, 3, 2);
mainLayout->addWidget (ProTelLabel, 4, 0);
mainLayout->addWidget (ProTelLineEdit, 4, 1);
//设定最优化显示,并且使用户无法改变对话框的大小.所谓最优化显示,即控件都按其SizeHint()的大小显示
mainLayout->setSizeConstraint (QLayout::SetFixedSize);
}
detail.h
#ifndef DETAIL_H
#define DETAIL_H
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QTextEdit>
#include <QComboBox>
#include <QGridLayout>
class Detail : public QWidget
{
Q_OBJECT
public:
explicit Detail(QWidget *parent = 0);
private:
QLabel *NationalLabel; //定义一个国家标签指针
QLabel *ProvinceLabel; //定义一个省份标签指针
QLabel *CityLabel; //定义一个城市标签指针
QLabel *IntroductLabel; //定义一个个人说明指针
QComboBox *NationalComboBox;
QComboBox *ProviceComboBox;
QLineEdit *CityLineEdit;
QTextEdit *IntroductTextEdit;
QGridLayout *mainLayout;
};
#endif // DETAIL_H
detail.cpp
#include "detail.h"
Detail::Detail(QWidget *parent) : QWidget(parent)
{
NationalLabel = new QLabel(tr("国家/地址:"));
NationalComboBox = new QComboBox;
NationalComboBox->insertItem (0, tr("中国"));
NationalComboBox->insertItem (1, tr("美国"));
NationalComboBox->insertItem (2, tr("法国"));
NationalComboBox->insertItem (3, tr("英国"));
ProvinceLabel = new QLabel(tr("省份:"));
ProviceComboBox = new QComboBox;
ProviceComboBox->insertItem (0, tr("河南省"));
ProviceComboBox->insertItem (1, tr("河北省"));
ProviceComboBox->insertItem (2, tr("广西省"));
ProviceComboBox->insertItem (3, tr("山东省"));
CityLabel = new QLabel(tr("城市:"));
CityLineEdit = new QLineEdit;
IntroductLabel = new QLabel(tr("个人说明:"));
IntroductTextEdit = new QTextEdit;
mainLayout = new QGridLayout(this);
mainLayout->setMargin (15);
mainLayout->setSpacing (10);
mainLayout->addWidget (NationalLabel, 0, 0);
mainLayout->addWidget (NationalComboBox, 0, 1);
mainLayout->addWidget (ProvinceLabel, 1, 0);
mainLayout->addWidget (ProviceComboBox, 1, 1);
mainLayout->addWidget (CityLabel, 2, 0);
mainLayout->addWidget (CityLineEdit, 2, 1);
mainLayout->addWidget (IntroductLabel, 3, 0);
mainLayout->addWidget (IntroductTextEdit, 3, 1);
}