Skip to content

QIODevice

ModuleCore
Include
#include <QCoroIODevice>
CMake
target_link_libraries(myapp QCoro::Core)
Inherited ByQCoroProcess, QCoroAbstractSocket, QCoroLocalSocket, QCoroNetworkReply
class QCoroIODevice

QIODevice has several different IO operations that can be waited on asynchronously. Since QIODevice itself doesn't provide the abaility to co_await those operations, QCoro provides a wrapper class called QCoroIODevice. To wrap a QIODevice into a QCoroIODevice, use qCoro():

QCoroIODevice qCoro(QIODevice &);
QCoroIODevice qCoro(QIODevice *);

Note that Qt provides several subclasses of QIODevice. QCoro provides coroutine-friendly wrappers for some of those types as well (e.g. for QLocalSocket). This subclass can be passed to qCoro() function as well. Oftentimes the wrapper class will provide some additional features (like co_awaiting establishing connection etc.). You can check whether QCoro supports the QIODevice subclass by checking the list of supported Qt types.

readAll()

Waits until there are any data to be read from the device (similar to waiting until the device emits QIODevice::readyRead() signal) and then returns all data available in the buffer as a QByteArray. Doesn't suspend the coroutine if there are already data available in the QIODevice or if the QIODevice is not opened for reading.

This is the default operation when co_awaiting an instance of a QIODevice directly. Thus, it is possible to just do

const QByteArray content = co_await device;

instead of

const QByteArray content = qCoro(device).readAll();

See documentation for QIODevice::readAll() for details.

Awaitable auto QCoroIODevice::readAll();

read()

Waits until there are any data to be read from the device (similar to waiting until the device emits QIODevice::readyRead() signal) and then returns up to maxSize bytes as a QByteArray. Doesn't suspend the coroutine if there are already data available in the QIODevice or if the device is not opened for reading.

See documentation for QIODevice::read() for details.

Awaitable auto QCoroIODevice::read(qint64 maxSize = 0);

readLine()

Repeatedly waits for data to arrive until it encounters a newline character, end-of-data or until it reads maxSize bytes. Returns the resulting data as QByteArray.

See documentation for QIODevice::readLine() for details.

Awaitable auto QCoroIODevice::readLine(qint64 maxSize = 0)

Examples

const QByteArray data = co_await qCoro(device).readAll();