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;
}
相关推荐
2501_937860943 分钟前
从JDBC到数据访问:Java数据库编程完全指南
java·开发语言·数据库
FfHUCisI4 分钟前
Golang SSA 中间表示与优化 Pass
开发语言·后端·golang
JacksonMx9 分钟前
Java 服务调用下游接口注意点
java·开发语言
兔兔兔兔116 分钟前
记录C++ 11
开发语言·c++
码匠许师傅23 分钟前
【C++ 面试真题】32. 聊聊 C++ 的原子变量与无锁编程
java·c++·面试
撩得Android一次心动27 分钟前
Kotlin 语言【知识点整理2】
android·开发语言·kotlin
不会就选b31 分钟前
Linux之线程池(一)
java·开发语言
wabs66633 分钟前
关于栈【力扣150.逆波兰表达式求值的思考】
数据结构·c++·算法·leetcode··代码随想录
欧特克_Glodon41 分钟前
OpenCV计算机视觉开发入门与实践<十七>:点运算与灰度变换概述
c++·人工智能·opencv·计算机视觉
牛油果子哥q1 小时前
C++序列式容器深度精讲:vector/list/deque底层实现、扩容原理、迭代器失效、性能对比、工程选型避坑
开发语言·c++·list