How to make an asynchronous io file in qt?

I was wondering how to achieve async io file in qt? Is this even achievable in vanilla qt or does someone need to use another library (like libuv) to achieve something like this? I looked at QDataStream, but although it is a "stream", it does not block. I guess one solution would be to create a custom QIODevice that uses libuv internally, which can then be used with QDataStream, but not sure where to start. Any ideas?

Thanks for the help provided.

+2
source share
1 answer

I would execute a thread that will handle I / O. You can connect the appropriate sig / slots to β€œcall” the IO from the main stream to the I / O stream. You can transfer the data you need to read / write as a signal parameter. Something like that:

class FileIOThread : public QThread { public: void run(); public slots: void writeData(QByteArray &) void readData(QByteArray &) }; class MyClass { private: FileIOThread m_writerThread; signals: void sendData(QByteArray &); .... }; MyClass::MyClass() { connect(this, SIGNAL(sendData(QByteArray&)), &m_writerThread,SLOT(writeData(QByteArray&))); .... } 
+2
source

Source: https://habr.com/ru/post/1239702/


All Articles