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

读取文件:

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

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 假死状态。

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;
}

使用到的函数:

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值