Unsolved QNetworkAccessManager: Associate response with request
-
Hello,
I try to do a simple GET to a REST Service and want to know how I can identify/associate the request to the response I get in my SLOT function. For example I might send multiple requests the same manager and want to pass a messageID in order to read that ID in my SLOT function to associate the response with the responsible request.
Any ideas how to do this?
QNetworkAccessManager manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply)),
this, SLOT(replyFinished(QNetworkReply*)));manager->get(QNetworkRequest(QUrl("http://qt-project.org")));
-
You have two strategies:
- the QNetworkAccessManager::get() method returns a pointer to the QNetworkReply object instance that will handle the request so you could keep a container of the pointers and compare with the one returned in the finished signal,
- you can connect the finished signal of the returned QNetworkReply object instance to some slot on an object instance that is specific to that request.
-
To add to what @bsomervi said. QNetworkReply is a QObject, so instead of keeping a list of them you can use its setProperty method to mark the object returned from
get()
with whatever meta-data you want and use that to identify the request. -
@Chris-Kawa Sounds good, but how can I ensure that the SLOT function is called after the setProperty method is executed?
-
I'm not sure what you mean. You made a connect so the slot will get executed. What's the problem?
-
@Chris-Kawa so after the get, I set a property of the returned QNetworkReply object. Then in the the SLOT function I can access the property, is that the idea?
-
Yes, that's it.