T
T
terminator-light2018-04-26 13:52:09
Qt
terminator-light, 2018-04-26 13:52:09

How to implement request completion wait with QNetworkAccessManager in Qt?

There are examples on the Internet on how to implement it by connecting the slot to the QNetworkReply::finished signal with QEventLoop, but I did not find it with QNetworkAccessManager::replyFinished. Inside the handler, after receiving the data, another request is sent based on them and this is repeated.
I also wanted to know how to set a timeout. If it fails to connect within 3 seconds, it sends another request.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ighor July, 2018-04-26
@terminator-light

QNetworkAccessManager manager;
QEventLoop loop;
QObject::connect(&manager, QOverload<QNetworkReply*>::of(&QNetworkAccessManager::finished),
                 &loop, &QEventLoop::quit);
...
loop.exec();

And for a timeout, you can call QTimer::singleShot(3000, &loop, &QEventLoop::quit);
PS: locks should be avoided altogether, and everything should be handled in class slots. Note that one instance of QNetworkAccessManager is limited to four simultaneous connections.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question