Reputation: 14071
Qt documentation says that one QNetworkAccessManager
is to be used per application:
One QNetworkAccessManager should be enough for the whole Qt application.
However, I access multiple web services and need to call individual functions for parsing each of those:
As I have only one QNetworkAccessManager::finished(QNetworkReply * reply)
signal, what is the best way to dispatch the replies to its individual parsing function?
I could connect to the finished
signal of each QNetworkReply
object when I create the request:
QNetworkReply * QNetworkAccessManager::get(const QNetworkRequest & request)
But is it the best way to connect to a signal which only will be used once (performance)? Are there better ways?
Related own questions:
Upvotes: 0
Views: 1158
Reputation: 29285
You can use QNetworkRequest::setOriginatingObject(QObject * object)
method in order to attach a reference of the originating object to the request.
Suppose we have two objects issuing network requests. In this case, you can use the following code to distinguish replies from each other.
QNetworkRequest *req1 = new QNetworkRequest();
req1->setOriginatingObject(sender1);
networkAccessManager->get(req1);
QNetworkRequest *req2 = new QNetworkRequest();
req2->setOriginatingObject(sender2);
networkAccessManager->get(req2);
And in your slot:
if(reply->request().originatingObject() == sender1){
sender1->handle(...);
}
if(reply->request().originatingObject() == sender2){
sender2->handle(...);
}
Upvotes: 3