Horst Walter
Horst Walter

Reputation: 14071

How to dispatch replies from one QNetworkAccessManager?

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:

  1. Best way to share cookies within Qt application
  2. What is the correct way to "stop / shutdown" a QNetworkAccessManager?
  3. QNetworkAccessManager get/post from different thread possible?

Upvotes: 0

Views: 1158

Answers (1)

frogatto
frogatto

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

Related Questions