Qt demo——修改用户资料窗口
创始人
2025-05-30 15:40:15
0

一、效果展示

基本信息界面
在这里插入图片描述

联系方式界面
在这里插入图片描述
详细资料界面

在这里插入图片描述

二、实现

1.窗口布局

在这里插入图片描述

  • 左边是一个 QListWidget,分别包含三个 item。(基本信息,联系方式,详细资料
  • 右边整体是一个 QVBoxLayout,即垂直布局。
    • 上面是一个 QStackWidget,包含三个不同的页面(baseInfocontactdetail),每个页面都有自己的布局。
    • 上面是一个 QHBoxLayout,即水平布局。里面包含两个按钮(修改按钮 modifyBtn和 关闭按钮closeBtn)。
  • 最外层是一个分割窗口 QSplitter,将窗口分为左右两边。

2.实现步骤与代码

第一步:创建项目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
content.h中继承的父类改为 QFrame,头文件也改成 #include

在这里插入图片描述
conten.cpp里面的 QDialog也改成 QFrame

在这里插入图片描述

第二步:添加三个页面类 BaseInfoContactDetail

添加 BaseInfo

点击 add new添加新文件。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

再依次添加 ContactDetail类。

第三步:导航页实现

打开 content.h,类声明中包含三个自定义页面对象 ,两个按钮对象 和 一个堆栈窗体对象。

#ifndef CONTENT_H
#define CONTENT_H#include 
#include 
#include #include "baseinfo.h"
#include "contact.h"
#include "detail.h"class Content : public QFrame
{Q_OBJECTpublic:Content(QWidget *parent = nullptr);~Content();//堆栈窗体QStackedWidget * stkWidget;//基本信息页面BaseInfo * baseInfo;//联系方式页面Contact * contact;//详情页面Detail * detail;//修改按钮 和 关闭按钮QPushButton * modifyBtn;QPushButton * closeBtn;};
#endif // CONTENT_H

打开 content.cpp添加如下代码。

#include "content.h"
#include Content::Content(QWidget *parent): QFrame(parent)
{stkWidget = new QStackedWidget(this);stkWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);//插入三个页面baseInfo = new BaseInfo();contact = new Contact();detail = new Detail();stkWidget->addWidget(baseInfo);stkWidget->addWidget(contact);stkWidget->addWidget(detail);//对两个按钮进行布局modifyBtn = new QPushButton(tr("修改"));closeBtn = new QPushButton(tr("关闭"));QHBoxLayout * btnLayout = new QHBoxLayout();btnLayout->addStretch(1);btnLayout->addWidget(modifyBtn);btnLayout->addWidget(closeBtn);//对右边整体进行布局QVBoxLayout * rightLayout = new QVBoxLayout(this);rightLayout->setMargin(10);rightLayout->setSpacing(6);rightLayout->addWidget(stkWidget);rightLayout->addLayout(btnLayout);}Content::~Content()
{
}

第四步:用户基本信息界面实现

打开 baseinfo.h头文件,添加如下代码。

#ifndef BASEINFO_H
#define BASEINFO_H#include 
#include 
#include 
#include 
#include 
#include 
#include class BaseInfo : public QWidget
{Q_OBJECT
public:explicit BaseInfo(QWidget *parent = nullptr);/* 左侧 */QLabel * userNameLabel;QLabel * nameLabel;QLabel * sexLabel;QLabel * departmentLabel;QLabel * ageLabel;QLabel * noteLabel;QLineEdit * userNameLineEdit;QLineEdit * nameLineEdit;QComboBox * sexComboBox;QTextEdit * departmentTextEdit;QLineEdit * ageLineEdit;//左侧布局QGridLayout * leftLayout;/*  右侧  */QLabel * profilePhotoLabel;QLabel * profilePhotoIconLabel;QPushButton * updateBtn;//右上区域布局QHBoxLayout * topRightLayout;QLabel * profileLabel;QTextEdit * profileTextEdit;//右侧布局QVBoxLayout * rightLayout;signals:};#endif // BASEINFO_H

添加头像资源文件。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
随便选择一张头像。

在这里插入图片描述

再打开 baseinfo.cpp,添加如下代码。


#include "baseinfo.h"
#include 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("年龄:"));noteLabel = new QLabel(tr("备注:"));//设置备注样式noteLabel->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(noteLabel,5,0,1,2);//设置两列的比例为 1 : 3leftLayout->setColumnStretch(0,1);leftLayout->setColumnStretch(1,3);/*  右侧  */profilePhotoLabel = new QLabel(tr("头像:"));profilePhotoIconLabel = new QLabel();QPixmap profileIcon(":/C:/Users/hp/Pictures/Saved Pictures/121.jpg");profilePhotoIconLabel->setPixmap(profileIcon);profilePhotoIconLabel->resize(profileIcon.width(),profileIcon.height());updateBtn = new QPushButton(tr("更新"));//右上区域布局topRightLayout = new QHBoxLayout();topRightLayout->setSpacing(20);topRightLayout->addWidget(profilePhotoLabel);topRightLayout->addWidget(profilePhotoIconLabel);topRightLayout->addWidget(updateBtn);profileLabel = new QLabel(tr("个人说明:"));profileTextEdit = new QTextEdit;//右侧布局rightLayout = new QVBoxLayout();rightLayout->setMargin(10);rightLayout->addLayout(topRightLayout);rightLayout->addWidget(profileLabel);rightLayout->addWidget(profileTextEdit);/*  主布局  */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);}

第五步:联系方式页面实现

打开 contact.h头文件,添加如下代码。

#ifndef CONTACT_H
#define CONTACT_H#include 
#include 
#include 
#include 
#include class Contact : public QWidget
{Q_OBJECT
public:explicit Contact(QWidget *parent = nullptr);QLabel * emailLabel;QLabel * addressLabel;QLabel * postalCodeLabel;QLabel * mobilePhoneLabel;QCheckBox * messageCheckBox;QLabel * bussinessPhoneLabel;QLineEdit * emailLineEdit;QLineEdit * addressLineEdit;QLineEdit * postalCodeLineEdit;QLineEdit * mobilePhoneLineEdit;QLineEdit * bussinessPhoneLineEdit;QGridLayout * mainLayout;signals:};#endif // CONTACT_H

打开 contact.cpp,添加如下代码。

#include "contact.h"Contact::Contact(QWidget *parent) : QWidget(parent)
{emailLabel = new QLabel(tr("电子邮件:"));emailLineEdit = new QLineEdit;addressLabel = new QLabel(tr("联系地址:"));addressLineEdit = new QLineEdit;postalCodeLabel = new QLabel(tr("邮政编码:"));postalCodeLineEdit = new QLineEdit;mobilePhoneLabel = new QLabel(tr("移动电话:"));mobilePhoneLineEdit = new QLineEdit;messageCheckBox = new QCheckBox(tr("接收留言"));bussinessPhoneLabel = new QLabel(tr("办公电话:"));bussinessPhoneLineEdit = new QLineEdit;mainLayout = new QGridLayout(this);//布局mainLayout->setMargin(15);mainLayout->setSpacing(10);mainLayout->addWidget(emailLabel,0,0);mainLayout->addWidget(emailLineEdit,0,1);mainLayout->addWidget(addressLabel,1,0);mainLayout->addWidget(addressLineEdit,1,1);mainLayout->addWidget(postalCodeLabel,2,0);mainLayout->addWidget(postalCodeLineEdit,2,1);mainLayout->addWidget(mobilePhoneLabel,3,0);mainLayout->addWidget(mobilePhoneLineEdit,3,1);mainLayout->addWidget(messageCheckBox,3,2);mainLayout->addWidget(bussinessPhoneLabel,4,0);mainLayout->addWidget(bussinessPhoneLineEdit,4,1);mainLayout->setSizeConstraint(QLayout::SetFixedSize);
}

第六步:详细资料页面实现

打开 detail.h头文件,添加如下代码。

#ifndef DETAIL_H
#define DETAIL_H#include 
#include 
#include 
#include 
#include 
#include class Detail : public QWidget
{Q_OBJECT
public:explicit Detail(QWidget *parent = nullptr);QLabel * nationLabel;QLabel * provinceLabel;QLabel * cityLabel;QLabel * profileLabel;QComboBox * nationComboBox;QComboBox * provinceComboBox;QLineEdit * cityLineEdit;QTextEdit * profileEdit;QGridLayout * mainLayout;signals:};#endif // DETAIL_H

打开 detail.cpp文件,添加如下代码。


#include "detail.h"Detail::Detail(QWidget *parent) : QWidget(parent)
{nationLabel = new QLabel(tr("国家/地址:"));nationComboBox = new QComboBox();nationComboBox->addItem(tr("中国"));nationComboBox->addItem(tr("日本"));nationComboBox->addItem(tr("俄罗斯"));nationComboBox->addItem(tr("美国"));nationComboBox->addItem(tr("英国"));provinceLabel = new QLabel(tr("省份:"));provinceComboBox = new QComboBox();provinceComboBox->addItem(tr("四川省"));provinceComboBox->addItem(tr("贵州省"));provinceComboBox->addItem(tr("山西省"));provinceComboBox->addItem(tr("湖南省"));provinceComboBox->addItem(tr("广东省"));provinceComboBox->addItem(tr("江苏省"));cityLabel = new QLabel(tr("城市:"));cityLineEdit = new QLineEdit();profileLabel = new QLabel(tr("个人说明:"));profileEdit = new QTextEdit();//布局mainLayout = new QGridLayout(this);mainLayout->setMargin(15);mainLayout->setSpacing(10);mainLayout->addWidget(nationLabel,0,0);mainLayout->addWidget(nationComboBox,0,1);mainLayout->addWidget(provinceLabel,1,0);mainLayout->addWidget(provinceComboBox,1,1);mainLayout->addWidget(cityLabel,2,0);mainLayout->addWidget(cityLineEdit,2,1);mainLayout->addWidget(profileLabel,3,0);mainLayout->addWidget(profileEdit,3,1);mainLayout->setSizeConstraint(QLayout::SetFixedSize);
}

第七步:编写主函数

#include "content.h"#include 
#include 
#include 
#include 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,&QListWidget::currentRowChanged,content->stkWidget,&QStackedWidget::setCurrentIndex);//设置主布局框 即 水平分割窗口的标题splitterMain->setWindowTitle(QObject::tr("修改用户资料"));//设置主布局框 即 水平分割窗口的最小尺寸splitterMain->setMinimumSize(splitterMain->minimumSize());//设置主布局框 即 水平分割窗口的最大尺寸splitterMain->setMaximumSize(splitterMain->maximumSize());//显示主布局框 上面的控件也会一同显示splitterMain->show();//Content w;//w.show();return a.exec();
}

相关内容

热门资讯

【全民Python】Pytho... 目录 一.编辑器相关 1.代码自动格式化设置 2.vscode python 第三方库自动补全 第三...
Go语言入门【11】接口 接口 在go语言中,接口是一种抽象的类型,它把所有的具有共性的方法定义在...
kylin的介绍 Kylin是一个开源的分布式分析引擎,主要用于快速查询大数据集合。 概念 Kylin是...
项目质量管理工作 不得不重视的...         1、三大视角确保项目质量         我们需要从客户视角、SOW视角和组织视角三...
代码随想录算法训练营第四十八天... LeetCode 198 打家劫舍题目链接:https://leetcode.cn/p...
Leetcode第五天动态规划... 来源:力扣(LeetCode) 链接:htt...
蓝桥杯Web前端练习-----... 介绍 相信做过前端开发的小伙伴们对渐变色在 UI 设计中的流行度一定不陌生,网页上也时...
Ubuntu系统与Linux常... 目录一、Ubuntu系统:1. Ubuntu目录的简介2. Ubuntu与人交互3. ...
spark中distinct函... spark中的distinct函数去重方式和Scala中的distinct是不同的。 首先来看Sca...
数据分析师CDA认证 Leve... **黑色字体部分为考纲,蓝色字体部分为笔记,仅供参考 PART 1 数据...
云桌面技术哪家强?亲身体验后才... 一. 简介 作为一家领先的云计算服务提供商,华为云提供了丰富的云计算服务,...
axios.intercept... axios.interceptors.request.use返回config axios.inter...
如何在Linux中自定义定时调... 内容目录一、定时调度任务Cron的运行机制二、处理任务之间的依赖关系三、处理多任务之间的依赖关系 ...
大数据方向相关书籍重点知识总结 文章目录《Spark快速大数据分析》《Python数据科学手册》《Hadoop权威指南》《大数据&#...
03- 算法和算法分析 - 算... 程序执行时所需存储空间包括以下两部分: 固定部分,这部分空间的大小与输入/输出的数据的个数多少、数值...
GC 垃圾回收机制 文章目录JVM 的内存模型对象存活?引用计数算法可达性分析算法垃圾收集标记-清除算法标...
辉煌优配|危机即转机?摩根士丹... 摩根士丹利策略师表明,银行体系遭受压力意味着美股行将迎来熊市尾声,但这个...
day12函数进阶总结 Scope Of Variable 1. 变量作用域 变量作用域指的是变量定义完成后可以使用的有效范...
零基础转行软件测试入门指南 众所周知,互联网行业的高薪红利让人趋之若鹜,许多外行小白都选择学软件测试...
Java SE API kno... Java SE API know how 缓冲I/O InputStream.read() Outp...