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

读取文件:

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

cpp 复制代码
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 假死状态。

cpp 复制代码
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;
}

使用到的函数:

cpp 复制代码
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;
}
相关推荐
Chef_Chen23 分钟前
从0开始学习R语言--Day39--Spearman 秩相关
开发语言·学习·r语言
不学会Ⅳ30 分钟前
Mac M芯片搭建jdk源码环境(jdk24)
java·开发语言·macos
2401_8812444034 分钟前
牛客周赛99
c++
好开心啊没烦恼2 小时前
Python 数据分析:计算,分组统计1,df.groupby()。听故事学知识点怎么这么容易?
开发语言·python·数据挖掘·数据分析·pandas
lljss20202 小时前
Python11中创建虚拟环境、安装 TensorFlow
开发语言·python·tensorflow
山登绝顶我为峰 3(^v^)33 小时前
如何录制带备注的演示文稿(LaTex Beamer + Pympress)
c++·线性代数·算法·计算机·密码学·音视频·latex
Python×CATIA工业智造5 小时前
Frida RPC高级应用:动态模拟执行Android so文件实战指南
开发语言·python·pycharm
十五年专注C++开发6 小时前
CMake基础:条件判断详解
c++·跨平台·cmake·自动化编译
我叫小白菜6 小时前
【Java_EE】单例模式、阻塞队列、线程池、定时器
java·开发语言
狐凄7 小时前
Python实例题:基于 Python 的简单聊天机器人
开发语言·python