当前位置: 首页 > news >正文

Qt 序列化函数和反序列化函数

文章目录

  • 界面
  • 学生类
  • 序列化函数
  • 反序列化函数
  • 刷新所选择的下拉表值
  • 添加

在这里插入图片描述

界面

在这里插入图片描述

学生类

// 创建学生信息类
class studentInfo
{
public:QString id;     // 学号QString name;   // 学生姓名QString age;        // 学生年龄// 重写QDataStream& operator<<操作符,做数据序列化操作friend QDataStream& operator<<(QDataStream &stream, const studentInfo &student){// 将数据输入流对象中stream << student.id;stream << student.name;stream << student.age;return stream;}// 重写QDataStream& operator>>操作符,做数据反序列化操作friend QDataStream& operator>>(QDataStream &stream, studentInfo &student){// 从流对象中输出数据到学生结构体引用中stream >> student.id;stream >> student.name;stream >> student.age;return stream;}
};

序列化函数

将其放入容器内方便便利每一个学生参数的值

 QList<studentInfo> list;studentInfo student;

获取文本编辑的值读取到序列化文件中进行保存

//保存
void MainWindow::on_pushButton_clicked()
{studentInfo student;//序列化为二进制文件存在本地QFile file(QApplication::applicationDirPath()+"/"+"student.st");           //定义文件路径file.open(QIODevice::WriteOnly); //以只写模式打开QDataStream out(&file);          //定义数据流student.id=ui->m_Id->text();student.age=ui->m_Age->text();student.name=ui->m_Name->text();list.append(student);out<<list;qDebug()<<list[0].id;file.close();qDebug()<<(QString("OK"));ReShowCombox();
}

刷新下拉表的值

void MainWindow::ReShowCombox()
{// 获取 studentInfo 对象if(list.size()>0){ui->comboBox->addItem(list.last().name);}
}

反序列化函数

读取文件的值,添加到下拉列表中

void MainWindow::GetFile()
{studentInfo student;//反序列化本地二进制文件到程序中QFile file(QApplication::applicationDirPath()+"/"+"student.st");//文件在程序运行目录下if(file.exists())//如果文件存在,则从文件读取数据{file.open(QIODevice::ReadOnly);QDataStream in(&file);in>>list;file.close();}// 获取 studentInfo 对象if(list.size()>0){student.id=list.at(0).id;student.age=list.at(0).age;student.name=list.at(0).name;ui->m_Id->setText(student.id);ui->m_Age->setText(student.age);ui->m_Name->setText(student.name);for(int i=0;i<list.size();i++){ui->comboBox->addItem(list.at(i).name);}}}

刷新所选择的下拉表值

连接信号与槽函数

 connect(ui->comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MainWindow::on_comboBox_currentIndexChanged);

获取到当前的值

void MainWindow::on_comboBox_currentIndexChanged(int index)
{if (index >= 0) {// 获取当前选中项的 studentInfo 对象studentInfo student = list.at(index);// 刷新其他控件的值ui->m_Id->setText(student.id);ui->m_Name->setText(student.name);ui->m_Age->setText(student.age);}
}

添加

void MainWindow::on_pushButton_2_clicked()
{ui->m_Id->setText("");ui->m_Name->setText("");ui->m_Age->setText("");}
http://www.lryc.cn/news/210574.html

相关文章:

  • Linux之线程池
  • MAC安装stable diffusion
  • FPGA_状态机工作原理
  • 【python练习】python斐波那契数列超时问题
  • SpringCloud 微服务全栈体系(五)
  • msvcp140.dll丢失的正确解决方法
  • go pprof 如何使用 --chatGPT
  • 大数据可视化BI分析工具Apache Superset实现公网远程访问
  • 软考系统架构师知识点集锦二:软件工程
  • Go并发:使用sync.Pool来性能优化
  • git stash的使用方法
  • 【影刀演示_发送邮件的格式化HTML留存】
  • 深度学习(4)---生成式对抗网络(GAN)
  • ThinkPad电脑HDMI接口失灵如何解决?
  • 第四部分:JavaScript
  • 【游戏开发】【心法】游戏设计心法系列1-以玩法为核心去设计游戏
  • chrome谷歌浏览器取消网页所有剪切板的授权方法步骤
  • 目标检测算法改进系列之嵌入Deformable ConvNets v2 (DCNv2)
  • 最新发布!阿里云卓越架构框架重磅升级
  • 如何监听/抓取两个设备/芯片之间“UART串口”通信数据--监视TXD和RXD
  • JDK项目分析的经验分享
  • Java创建一个长度为10的数组,利用Arrays.sort(), 为数组元素排序
  • python 动态加载C# 动态库的一些问题
  • 代码审计-锐捷NBR路由器 EWEB网管系统 远程命令执行
  • VBA技术资料MF75:测量所选单元格范围的高度和宽度
  • 力扣 26. 删除有序数组中的重复项
  • 【uniapp】仿微信支付界面
  • windows + ubuntu + vscode开发环境配置安装
  • 设计模式:责任链模式(C#、JAVA、JavaScript、C++、Python、Go、PHP)
  • koa搭建服务器(二)