第三章 Qt5布局管理——综合实例:修改用户资料

目录

一、修改用户资料------分析

二、课本中的小bug

三、程序代码

content.h

content.cpp

baseinfo.h

baseinfo.cpp

contant.h

content.cpp

detail.h

detail.cpp

main.cpp


一、修改用户资料------分析

最外层是一个分割窗体QSplitter,分割窗体的左侧是一个QListWidget,右侧是一个QVBoxLayout布局,此布局包括一个堆栈窗体QStackWidget和一个按钮布局。在此堆栈窗体中包含三个页面,每个页面采用基本布局方式进行布局管理。

下图是运行结果:

二、课本中的小bug

在创建项目"Example"(书中"修改用户资料"的名字),基类选择"QDialog",在之后main.cpp按照书上编写完成运行,堆栈的内容不显示。这里在**创建Example时基类选择"QWidget"**就会正常显示了(本人用的Qt 版本是5.9.6)。下图是bug显示结果:

三、程序代码

content.h

cpp 复制代码
#ifndef CONTENT_H
#define CONTENT_H

#include <QDialog>
#include <QStackedWidget>
#include <QLayout>
#include <QPushButton>
#include "baseinfo.h"
#include "contact.h"
#include "detail.h"
#include <QSplitter>

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

content.cpp

cpp 复制代码
#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()
{

}

baseinfo.h

cpp 复制代码
#ifndef BASEINFO_H
#define BASEINFO_H

#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QComboBox>
#include <QLayout>
#include <QTextEdit>
#include <QPixmap>

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

baseinfo.cpp

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("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);
}

contant.h

cpp 复制代码
#ifndef CONTACT_H
#define CONTACT_H

#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QGridLayout>
#include <QCheckBox>

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

content.cpp

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;
    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);
}

detail.h

cpp 复制代码
#include <QLayout>
#include <QTextEdit>

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

detail.cpp

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("英国"));
    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);
}

main.cpp

cpp 复制代码
#include "content.h"
#include <QApplication>
#include <QTextCodec>
#include <QSplitter>
#include <QListWidget>

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();
}
相关推荐
坊钰21 分钟前
【Java 数据结构】移除链表元素
java·开发语言·数据结构·学习·链表
chenziang127 分钟前
leetcode hot100 LRU缓存
java·开发语言
时雨h41 分钟前
RuoYi-ue前端分离版部署流程
java·开发语言·前端
云计算DevOps-韩老师1 小时前
【网络云计算】2024第52周-每日【2024/12/25】小测-理论&实操-自己构造场景,写5个系统管理的脚本-解析
开发语言·网络·云计算·bash·perl
暮色尽染1 小时前
Python 正则表达式
开发语言·python
IT猿手1 小时前
最新高性能多目标优化算法:多目标麋鹿优化算法(MOEHO)求解GLSMOP1-GLSMOP9及工程应用---盘式制动器设计,提供完整MATLAB代码
开发语言·算法·机器学习·matlab·强化学习
小爬虫程序猿1 小时前
利用Java爬虫获取速卖通(AliExpress)商品详情的详细指南
java·开发语言·爬虫
xlsw_1 小时前
java全栈day21--Web后端实战之利用Mybaits查询数据
java·开发语言
Murphy20232 小时前
.net4.0 调用API(form-data)上传文件及传参
开发语言·c#·api·httpwebrequest·form-data·uploadfile·multipart/form-
我曾经是个程序员2 小时前
C#Directory类文件夹基本操作大全
服务器·开发语言·c#