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

Qt 读写数据流文件(转 CppGuiProgrammingWithQt4)

读取文件:

update 20140525:添加线程处理,在读取大文件时优化,防止 app 出现 application 假死状态。

bool SpreadSheet::readFile(const QString &filePath){QFile file(filePath);if ( !file.open(QIODevice::ReadOnly)) {QMessageBox::warning(this, tr("Spreadsheet"),tr("Cannot read file %1:\n%2.").arg(file.fileName()).arg(file.errorString()));return false;}QDataStream in(&file);in.setVersion(QDataStream::Qt_5_3);quint64 magic;in >> magic;if (SpreadSheet::MagicNumber != magic) {QMessageBox::warning(this, tr("Spreadsheet"),tr("The file is not a Spreadsheet file."));return false;}clear();quint32 row;quint32 column;QString str;QProgressDialog* process =progressDialog(this, tr("Load %1").arg(file.fileName()), SpreadSheet::mMaxRow);process->setModal(true);QApplication::setOverrideCursor(Qt::WaitCursor);while ( !in.atEnd()) {in >> row >> column >> str;setFormula(row, column, str);process->setValue(row);if ( process->wasCanceled()) {clear();delete process;file.close();}}QApplication::restoreOverrideCursor();delete process;return true;
}

写入文件:

update 20140525:添加线程处理,在写入大文件时优化,防止 app 出现 application 假死状态。

bool SpreadSheet::writeFile(const QString &filePath){QFile file(filePath);if ( !file.open(QIODevice::WriteOnly)) {QMessageBox::warning(this, tr("Spreadsheet"),tr("Cannot write file %1:\n%2.").arg(file.fileName()).arg(file.errorString()));return false;}QDataStream out(&file);out.setVersion(QDataStream::Qt_5_3);out << (quint64) SpreadSheet::MagicNumber;QProgressDialog* progress =progressDialog(this, tr("Save %1").arg(file.fileName()), SpreadSheet::mMaxRow);progress->setModal(true);QApplication::setOverrideCursor(Qt::WaitCursor);QString str;for (int i(0); i != SpreadSheet::mMaxRow; ++i) {progress->setValue(i);qApp->processEvents(QEventLoop::ExcludeUserInputEvents);if ( progress->wasCanceled()) {file.remove();delete progress;return false;}for (int j(0); j != SpreadSheet::mMaxColumn; ++j) {str = formula(i, j);if ( !str.isEmpty()) {out << (quint32)i << (quint32)j << str;}}}delete progress;QApplication::restoreOverrideCursor();return true;
}

使用到的函数:

QProgressDialog* SpreadSheet::progressDialog(QWidget* widget, const QString &str, const int range){QProgressDialog* progressDialog(new QProgressDialog(widget));progressDialog->setLabelText(str);progressDialog->setRange(0, range);return progressDialog;
}

http://www.lryc.cn/news/190357.html

相关文章:

  • Pygame中将鼠标形状设置为图片2-2
  • GPU 基础知识整理
  • stable diffusion API接口 + 扩展接口
  • MySQL数据库基本操作和完整性约束类型详解
  • unity2022版本 实现加减进度条
  • COCO数据集中图像的caption读取到txt文件
  • 再谈Java泛型
  • scss使用自定义函数实现单位像素随屏幕比例动态缩放
  • Django 静态自定义化配置
  • TensorFlow入门(十四、数据读取机制(1))
  • hyperf框架WebSocket 服务
  • 前端模块化
  • 如何使用Docker轻松构建和管理应用程序(一)
  • uniapp 获取地理位置(uni#getLocation和高德sdk获取中文地址)
  • openmp 通用核心 学习 2 数据环境—任务-内存模型
  • Linux有哪些指令
  • 图扑 HT for Web 风格属性手册教程
  • oracle 数据库删除序列
  • JAVA毕业设计098—基于Java+Springboot的在线教育课程视频(源码+数据库)
  • 如何在雷电模拟器上安装Magisk并加载movecert模块抓https包(二)
  • 基于web的酒店客房管理系统
  • linux查看系统信息
  • 蓝牙官网demo的记录
  • Linux相关概念及常见指令
  • CentOS 系统如何在防火墙开启端口
  • 2023年电工(初级)证考试题库及电工(初级)试题解析
  • vue拦截器是什么,如何使用
  • CSS 之 table 表格布局
  • 【Kotlin精简】第2章 集合
  • VSCODE+PHP8.2配置踩坑记录