目录
一、修改用户资料——分析
二、课本中的小bug
三、程序代码
content.h
content.cpp
baseinfo.h
baseinfo.cpp
contant.h
content.cpp
detail.h
detail.cpp
main.cpp
最外层是一个分割窗体QSplitter,分割窗体的左侧是一个QListWidget,右侧是一个QVBoxLayout布局,此布局包括一个堆栈窗体QStackWidget和一个按钮布局。在此堆栈窗体中包含三个页面,每个页面采用基本布局方式进行布局管理。
下图是运行结果:
在创建项目"Example"(书中”修改用户资料“的名字),基类选择"QDialog",在之后main.cpp按照书上编写完成运行,堆栈的内容不显示。这里在创建Example时基类选择"QWidget"就会正常显示了(本人用的Qt 版本是5.9.6)。下图是bug显示结果:
#ifndef CONTENT_H
#define CONTENT_H
#include
#include
#include
#include
#include "baseinfo.h"
#include "contact.h"
#include "detail.h"
#include
class Content : public QWidget
{
Q_OBJECT
public:
Content(QWidget *parent = 0);
~Content();
QStackedWidget * stack;//堆栈对象
QPushButton * AmenBtn;//修改按钮
QPushButton * CloseBtn;//关闭按钮
BaseInfo * baseinfo;//基本信息对象
Contact * contact;//联系方式对象
Detail * detail;//详细信息对象
};
#endif // CONTENT_H
#include "content.h"
Content::Content(QWidget *parent)
: QWidget(parent)
{
stack = new QStackedWidget(this);//创建一个堆栈对象
//对堆栈窗口的的风格进行设置
stack->setFrameStyle(QFrame::Panel | QFrame::Raised);
/*
* QFrame::Raised:框架和内容物出现凸起;使用当前颜色组的浅色和深色绘制 3D 凸起线
* QFrame::Panel:框架和内容物与周围环境齐平;使用调色板绘制
*/
/* 插入三个页面 */
baseinfo = new BaseInfo();
contact = new Contact();
detail = new Detail();
stack->addWidget(baseinfo);
stack->addWidget(contact);
stack->addWidget(detail);
/* 创建两个按钮 */
AmenBtn = new QPushButton(tr("修改"));
CloseBtn = new QPushButton(tr("关闭"));
QHBoxLayout * BtnLayout = new QHBoxLayout;
BtnLayout->addStretch(1);
BtnLayout->addWidget(AmenBtn);
BtnLayout->addWidget(CloseBtn);
/* 进行整体布局 */
QVBoxLayout * RightLayout = new QVBoxLayout(this);
RightLayout->setMargin(10);//布局外部边界的宽度
RightLayout->setSpacing(6);//间隔
RightLayout->addWidget(stack);
RightLayout->addLayout(BtnLayout);
}
Content::~Content()
{
}
#ifndef BASEINFO_H
#define BASEINFO_H
#include
#include
#include
#include
#include
#include
#include
#include
class BaseInfo : public QWidget
{
Q_OBJECT
public:
explicit BaseInfo(QWidget *parent = nullptr);
signals:
public slots:
private:
//左侧
QLabel * UserNameLabel;
QLabel * NameLabel;
QLabel * SexLabel;
QLabel * DepartmentLabel;
QLabel * AgeLabel;
QLabel * OtherLabel;
QLineEdit * UserNameLineEdit;
QLineEdit * NameLineEdit;
QLineEdit * AgeLineEdit;
QComboBox * SexComboBox;
QTextEdit * DepartmentTextEdit;
QGridLayout * LeftLayout;
//右侧
QLabel * HeadLabel;
QLabel * HeadIconLabel;
QPushButton * UpdateHeadBtn;
QHBoxLayout * TopRightLayout;
QLabel * IntroductionLabel;
QTextEdit * IntroductionTextEdit;
QVBoxLayout * RightLayout;
};
#endif // BASEINFO_H
#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("color.ico");
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);
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
}
#ifndef CONTACT_H
#define CONTACT_H
#include
#include
#include
#include
#include
class Contact : public QWidget
{
Q_OBJECT
public:
explicit Contact(QWidget *parent = nullptr);
signals:
public slots:
private:
QLabel * EmailLabel;
QLineEdit * EmailLineEdit;
QLabel * AddrLabel;
QLineEdit * AddrLineEdit;
QLabel * CodeLabel;
QLineEdit * CodeLineEdit;
QLabel * MoviTelLabel;
QLineEdit * MoviTelLineEdit;
QCheckBox * MoviTelCheckBox;
QLabel * ProTelLabel;
QLineEdit * ProTelLineEdit;
QGridLayout * mainLayout;
};
#endif // CONTACT_H
#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;
MoviTelCheckBox = 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(MoviTelCheckBox,3,2);
mainLayout->addWidget(ProTelLabel,4,0);
mainLayout->addWidget(ProTelLineEdit,4,1);
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
}
#include
#include
class Detail : public QWidget
{
Q_OBJECT
public:
explicit Detail(QWidget *parent = nullptr);
signals:
public slots:
private:
QLabel * NationalLabel;
QComboBox * NationalComboBox;
QLabel * ProvinceLabel;
QComboBox * ProvinceComboBox;
QLabel * CityLabel;
QLineEdit * CityLineEdit;
QLabel * IntroductLabel;
QTextEdit * IntroduceTextEdit;
QGridLayout * mainLayout;
};
#endif // DETAIL_H
#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("英国"));
ProvinceLabel = new QLabel(tr("省份:"));
ProvinceComboBox = new QComboBox;
ProvinceComboBox->insertItem(0,tr("江省份"));
ProvinceComboBox->insertItem(1,tr("山东省"));
ProvinceComboBox->insertItem(2,tr("浙江省"));
CityLabel = new QLabel(tr("城市:"));
CityLineEdit = new QLineEdit;
IntroductLabel = new QLabel(tr("个人介绍:"));
IntroduceTextEdit = 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(ProvinceComboBox,1,1);
mainLayout->addWidget(CityLabel,2,0);
mainLayout->addWidget(CityLineEdit,2,1);
mainLayout->addWidget(IntroductLabel,3,0);
mainLayout->addWidget(IntroduceTextEdit,3,1);
}
#include "content.h"
#include
#include
#include
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFont font("黑体",12);
a.setFont(font);
//新建一个水平分割窗口对象,作为著布局框
QSplitter * splitterMain = new QSplitter(Qt::Horizontal,0);
splitterMain->setOpaqueResize(true);
//在水平分割窗口的左侧窗口中插入一个QListWidget作为一个条目选择框
QListWidget * list = new QListWidget(splitterMain);
list->insertItem(0,QObject::tr("基本信息"));
list->insertItem(1,QObject::tr("联系方式"));
list->insertItem(2,QObject::tr("详细信息"));
//在新建的水平分割窗的右侧窗口中插入Content类对象
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();
//Content w;
//w.show();
return a.exec();
}