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


    读取文件

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

    1. bool SpreadSheet::readFile(const QString &filePath){
    2. QFile file(filePath);
    3. if ( !file.open(QIODevice::ReadOnly)) {
    4. QMessageBox::warning(this, tr("Spreadsheet"),
    5. tr("Cannot read file %1:\n%2.")
    6. .arg(file.fileName())
    7. .arg(file.errorString()));
    8. return false;
    9. }
    10. QDataStream in(&file);
    11. in.setVersion(QDataStream::Qt_5_3);
    12. quint64 magic;
    13. in >> magic;
    14. if (SpreadSheet::MagicNumber != magic) {
    15. QMessageBox::warning(this, tr("Spreadsheet"),
    16. tr("The file is not a Spreadsheet file."));
    17. return false;
    18. }
    19. clear();
    20. quint32 row;
    21. quint32 column;
    22. QString str;
    23. QProgressDialog* process =
    24. progressDialog(this, tr("Load %1").arg(file.fileName()), SpreadSheet::mMaxRow);
    25. process->setModal(true);
    26. QApplication::setOverrideCursor(Qt::WaitCursor);
    27. while ( !in.atEnd()) {
    28. in >> row >> column >> str;
    29. setFormula(row, column, str);
    30. process->setValue(row);
    31. if ( process->wasCanceled()) {
    32. clear();
    33. delete process;
    34. file.close();
    35. }
    36. }
    37. QApplication::restoreOverrideCursor();
    38. delete process;
    39. return true;
    40. }

    写入文件

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

    1. bool SpreadSheet::writeFile(const QString &filePath){
    2. QFile file(filePath);
    3. if ( !file.open(QIODevice::WriteOnly)) {
    4. QMessageBox::warning(this, tr("Spreadsheet"),
    5. tr("Cannot write file %1:\n%2.")
    6. .arg(file.fileName())
    7. .arg(file.errorString()));
    8. return false;
    9. }
    10. QDataStream out(&file);
    11. out.setVersion(QDataStream::Qt_5_3);
    12. out << (quint64) SpreadSheet::MagicNumber;
    13. QProgressDialog* progress =
    14. progressDialog(this, tr("Save %1").arg(file.fileName()), SpreadSheet::mMaxRow);
    15. progress->setModal(true);
    16. QApplication::setOverrideCursor(Qt::WaitCursor);
    17. QString str;
    18. for (int i(0); i != SpreadSheet::mMaxRow; ++i) {
    19. progress->setValue(i);
    20. qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
    21. if ( progress->wasCanceled()) {
    22. file.remove();
    23. delete progress;
    24. return false;
    25. }
    26. for (int j(0); j != SpreadSheet::mMaxColumn; ++j) {
    27. str = formula(i, j);
    28. if ( !str.isEmpty()) {
    29. out << (quint32)i << (quint32)j << str;
    30. }
    31. }
    32. }
    33. delete progress;
    34. QApplication::restoreOverrideCursor();
    35. return true;
    36. }

    使用到的函数:

    1. QProgressDialog* SpreadSheet::progressDialog(QWidget* widget,
    2. const QString &str,
    3. const int range){
    4. QProgressDialog* progressDialog(new QProgressDialog(widget));
    5. progressDialog->setLabelText(str);
    6. progressDialog->setRange(0, range);
    7. return progressDialog;
    8. }

  • 相关阅读:
    MySQL的
    数据采集项目之业务数据(三)
    将nestjs项目迁移到阿里云函数
    云呐|机房监控服务平台,机房监控服务平台有哪些
    对指针的深入理解
    Al中秋节由来
    如何使用 Element UI? 以登录框为例带你感受一下基础使用
    CSS-线性渐变无畸变-环形普通进度条-环形能量块进度条-局部环形普通进度条
    HONEYWELL 05701-A-0325控制脉冲模块
    广义表的存储结构及其基本运算
  • 原文地址:https://blog.csdn.net/weiweiqiao/article/details/133800643