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

  • 相关阅读:
    gsteamer日志输出实例
    MyBatis的使用
    住友电工(常州)硬质合金有限公司项目 电力监控系统的设计与应用
    针对主机/云/容器/虚拟机的虚拟网络分路器-nTap
    基于VUE + Echarts 实现可视化数据大屏销售大数据
    Tomcat详解(全网最全,最好的)
    【CPP】数组名与指针
    AI创作教程之 如何在本地 PC 上运行稳定的 Diffusion 2.0 (无代码指南)
    vue 01
    CSMM软件能力成熟度评估
  • 原文地址:https://blog.csdn.net/weiweiqiao/article/details/133800643