• 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. }

  • 相关阅读:
    NIO- Handler业务处理器
    web基础与HTTP协议
    【UnityShader入门精要学习笔记】第十六章 Unity中的渲染优化技术 (上)
    (09_22)【有奖体验】轻点鼠标,让古籍数字化“重生_
    【数据结构】堆排序的实现
    前缀表达式和后缀表达式 - C++代码
    【JUC并发编程--java线程】
    代码随想录day57|647. 回文子串516. 最长回文子序列
    闭包学习记录-iOS开发
    Javaweb与Mybatis实现简单的用户注册登录
  • 原文地址:https://blog.csdn.net/weiweiqiao/article/details/133800643