且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从不同线程的QFile读取

更新时间:2022-01-29 23:00:50

并行读取是通过拆分请求和指示部分完成的:您首先请求数据,读取器读取数据,然后您对读取数据的指示做出反应。

Concurrent reading is done by splitting the request and indication parts: you first request the data, the reader reads it, then you react to the indication that a data was read.

由于文件对象是从工作线程访问的,我把它封装在 AData 类中。您可以将类移动到工作线程,您从外部调用的信号是线程安全的。

Since the file object is accessed from a worker thread, I've encapsulated it inside the AData class. You can move the class to a worker thread, the signals you call from outside are thread-safe.

也应该可以在 QFile ,但是正确地需要使用Qt的实现细节( core_private 模块)。

It should also be possible to make an asynchronous wrapper around a QFile, but doing it properly would require using Qt's implementation details (core_private module).

// https://github.com/KubaO/***n/tree/master/questions/async-file-io-39226814
#include <QtWidgets>

class AData : public QObject
{
    Q_OBJECT
    QFile m_file;
public:
    explicit AData(QObject * parent = nullptr) : QObject{parent} {
        connect(this, &AData::save, this, [=](const QByteArray & data, qint64 pos){
            m_file.seek(pos);
            m_file.write(data);
        });
        connect(this, &AData::load, this, [=](qint64 pos, qint64 len){
           m_file.seek(pos);
           if (len == -1) len = m_file.size();
           auto data = m_file.read(len);
           emit loaded(data, pos);
        });
    }
    bool open(const QString & name) {
        m_file.setFileName(name);
        return m_file.open(QIODevice::ReadWrite);
    }
    Q_SIGNAL void save(const QByteArray &data, qint64 pos = 0) const;
    Q_SIGNAL void load(qint64 pos, qint64 len) const;
    Q_SIGNAL void loaded(const QByteArray &data, qint64 pos) const;
};

测试UI演示如何使用它:

A test UI demonstrates how to use it:

int main(int argc, char ** argv) {
    QApplication app{argc, argv};
    struct Thread : QThread {
        ~Thread() { quit(); wait(); }
    } ioThread;
    AData data;
    data.open("myfile");
    data.moveToThread(&ioThread);
    ioThread.start();

    QWidget ui;
    QGridLayout layout{&ui};
    QTextEdit text;
    QPushButton load{"Load"};
    QPushButton save{"Save"};
    QPushButton clear{"Clear"};
    layout.addWidget(&text, 0, 0, 1, 2);
    layout.addWidget(&load, 1, 0);
    layout.addWidget(&save, 1, 1);
    layout.addWidget(&clear, 2, 0, 1, 2);
    ui.show();

    using Q = QObject;
    Q::connect(&load, &QPushButton::clicked, &data, [&]{
        data.load(0, -1);
    });
    Q::connect(&data, &AData::loaded, &app, [&](const QByteArray & data, qint64){
       text.setPlainText(QString::fromUtf8(data));
    });
    Q::connect(&save, &QPushButton::clicked, &data, [&]{
        data.save(text.document()->toPlainText().toUtf8());
    });
    Q::connect(&clear, &QPushButton::clicked, &text, &QTextEdit::clear);

    return app.exec();
}
#include "main.moc"