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;
}
相关推荐
Aderversa36 分钟前
C++开源协程库async_simple有栈协程源码分析
c++·协程
uyeonashi41 分钟前
【QT】窗口详解
开发语言·c++·qt·学习
Hello eveybody2 小时前
C++介绍整数二分与实数二分
开发语言·数据结构·c++·算法
空白木各2 小时前
evo工具
c++
编码小笨猪2 小时前
浅谈Linux中一次系统调用的执行过程
linux·服务器·c++
jmlinux3 小时前
从 C 语言计算器到串口屏应用
c语言·开发语言
Mallow Flowers3 小时前
Python训练营-Day31-文件的拆分和使用
开发语言·人工智能·python·算法·机器学习
云边小网安4 小时前
java集合篇(六) ---- ListIterator 接口
java·开发语言·青少年编程·java集合
不被定义的程序猿4 小时前
Golang 在 Linux 平台上的并发控制
开发语言·后端·golang
陈旭金-小金子5 小时前
发现 Kotlin MultiPlatform 的一点小变化
android·开发语言·kotlin