I am building a library with Qt that calls a server and I need to build a synchronous function that waits for an HTTP response (QNetworkReply object) and I am using a QEventLoop to achieve this. Currently the server gets called but the loop does not wait for the reply to finish, instead it carries on with an empty QNetworkReply object.
The exact same function works in a simple test project I built that only contains one thread and a call from main to this function. The reply is waited for and everything works as expected. But in my project that contains multiple threads the scenario described above occurs and the event loop does not wait for the reply. The request is sent to the server and shows up there but the response can't get back to the QNetworkReply object because the function has already executed.
Here is the network section of my function. In my project the statusCode variable always ends up being 0 and the reply is empty but in the simple test scenario they are 200 and the expected HTTP response.
QNetworkAccessManager* networkManager = new QNetworkAccessManager(this);
QNetworkReply* reply = networkManager->get(request);
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
qInfo() << "Http Get completed with status code:" << statusCode.toInt();