Skip to content

QNetworkReply

ModuleNetwork
Include
#include <QCoroNetworkReply>
CMake
target_link_libraries(myapp QCoro::Network)
InheritsQCoroIODevice

QNetworkReply has two asynchronous aspects: one is waiting for the reply to finish, and one for reading the response data as they arrive. QCoro supports both. QNetworkReply is a subclass of QIODevice, so you can leverage all the features of QCoroIODevice to asynchronously read data from the underlying QIODevice using coroutines.

To wait for the reply to finish, one can simply co_await the reply object:

QNetworkAccessManager nam;
auto *reply = co_await nam.get(request);

The QCoro frameworks allows co_awaiting on QNetworkReply objects. The co-awaiting coroutine is suspended, until QNetworkReply::finished() signal is emitted.

To make it work, include QCoroNetworkReply in your implementation.

#include <QCoroNetworkReply>

QCoro::Task<> MyClass::fetchData() {
    // Creates QNetworkAccessManager on stack
    QNetworkAccessManager nam;
    // Calls QNetworkAccessManager::get() and co_awaits on the returned QNetworkReply*
    // until it finishes. The current coroutine is suspended until that.
    auto *reply = co_await nam.get(QUrl{QStringLiteral("https://.../api/fetch")});
    // When the reply finishes, the coroutine is resumed and we can access the reply content.
    const auto data = reply->readAll();
    // Raise your hand if you never forgot to delete a QNetworkReply...
    delete reply;
    doSomethingWithData(data);
    // Extra bonus: the QNetworkAccessManager is destroyed automatically, since it's on stack.
}