Skip to content

QIODevice

ModuleCore
Include
#include <QCoroIODevice>
CMake
target_link_libraries(myapp QCoro::Core)
QMake
QT += QCoroCore
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.

QCoroIODevice::readAll() additionally accepts an optional timeout parameter. If no data become available within the timeout, the coroutine returns an empty QByteArray. If no timeout is specified or if it is set to -1, the operation will never time out.

QCoro::Task<QByteArray> QCoroIODevice::readAll();
QCoro::Task<QByteArray> QCoroIODevice::readAll(std::chrono::milliseconds timeout);

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.

QCoroIODevice::read() additionally accepts an optional timeout parameter. If no data become available within the timeout, the coroutine returns an empty QByteArray. If no timeout is specified or if it is set to -1, the operation will never time out.

QCoro::Task<QByteArray> QCoroIODevice::read(qint64 maxSize = 0);
QCoro::Task<QByteArray> QCoroIODevice::read(qint64 maxSize, std::chrono::milliseconds timeout);

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.

QCoroIODevice::readLine() additionally accepts an optional timeout parameter. If no data become available within the timeout, the coroutine returns an empty QByteArray. If no timeout is specified or if it is set to -1, the operation will never time out.

QCoro::Task<QByteArray> QCoroIODevice::readLine(qint64 maxSize = 0)
QCoro::Task<QByteArray> QCoroIODevice::readLine(qint64 maxSize, std::chrono::milliseconds timeout);

waitForReadyRead()

Waits for at most timeout_msecs milliseconds for data to become available for reading in the QIODevice. Returns true when the device becomes ready for reading within the given timeout. Returns false if the operation times out, if the device is not opened for reading or in any other state in which the device will never become ready for reading.

If the timeout is -1, the operation will never time out.

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

QCoro::Task<bool> QCoroIODevice::waitForReadyRead(int timeout_msecs = 30'000);
QCoro::Task<bool> QCoroIODevice::waitForReadyRead(std::chrono::milliseconds timeout);

waitForBytesWritten()

Waits for at most timeout_msecs milliseconds for data to be flushed from a buffered QIODevice. Returns std::optional<qint64>, which is empty if the operation has timed out, the device is not opened for writing or is in any other state in which the device will never be able to write any data. When the data are successfully flushed, returns number of bytes written.

If the timeout is -1, the operation will never time out.

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

QCoro::Task<std::optional<qint64>> QCoroIODevice::waitForBytesWritten(int timeout_msecs = 30'000);
QCoro::Task<std::optional<qint64>> QCoroIODevice::waitForBytesWritten(std::chrono::milliseconds timeout);

Examples

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