How to handle many QNetworkReplies ?
-
Hi.
I'm looking for the best (or least bad) way to handle all the QNetworkReply objects that are created.As I understand it I should do like this
this->myQNetworkReply = this->myQNetworkAccessManager->get( myQNetworkRequest); connect (this->myQNetworkReply, &QNetworkReply::finished, this, SLOT(some_slot_to_read_the result_from_myQNetworkReply));
Now, If I want to send 10 queries repeatedly how do I best do this?
Should I do something like this (I use a stupid while loop instead of using a QTimer to make the example code shorter)QNetworkReply* tmp; QList<QNetworkReply*> myList; while (1 == 1) { tmp = this->myQNetworkAccessManager->get(myQNetworkRequest1); myList.append(tmp); connect(tmp, ... like above); tmp = this->myQNetworkAccessManager->get(myQNetworkRequest2); myList.append(tmp); connect(tmp, ... like above); .... tmp = this->myQNetworkAccessManager->get(myQNetworkRequest10); myList.append(tmp); connect(tmp, ... like above); }
This seems like a lot of code, and a lot of connections are created . Should I do the connects, or should I instead iterate the QList and look for replies that isFinished() and then processes the data?
What is the best way? -
Does
some_slot_to_read_the result_from_myQNetworkReply
change from one request to the other? -
@VRonin
Good point.
The 10 requests are different so the slot functions must handle different data, so in theory they must be different.
So in theory, 10 different slot functions are needed.@OpenGL said in How to handle many QNetworkReplies ?:
The 10 requests are different so the slot functions must handle different data
So why don't you treat your only slot as a "factory" where you then call the other 10 methods based on some condition from the data received in the response?
In addition, I like to connect the QNetworkAccessManager::finished() signal instead of individual QNetworkReply::finished() signal
connect(&networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(handleCommandResponse(QNetworkReply*)));
-
@OpenGL said in How to handle many QNetworkReplies ?:
The 10 requests are different so the slot functions must handle different data
So why don't you treat your only slot as a "factory" where you then call the other 10 methods based on some condition from the data received in the response?
In addition, I like to connect the QNetworkAccessManager::finished() signal instead of individual QNetworkReply::finished() signal
connect(&networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(handleCommandResponse(QNetworkReply*)));
@Pablo-J.-Rogina said in How to handle many QNetworkReplies ?:
I like to connect the QNetworkAccessManager::finished() signal instead of individual QNetworkReply::finished() signal
I desagree. There should be only 1 QNAM per app and your slot would get call even when totally unrelated requests are triggered.
This is the equivalent of processing an event of a widget fromQApplication::event
instead ofQWidget::event
-
@VRonin maybe not being an English-native speaker made my comment not clear enough. Yes, I'm just using only one QNAM per application.
-
@VRonin maybe not being an English-native speaker made my comment not clear enough. Yes, I'm just using only one QNAM per application.
@Pablo-J.-Rogina said in How to handle many QNetworkReplies ?:
I'm just using only one QNAM per application.
That means that, if you connect to
QNetworkAccessManager::finished()
instead ofQNetworkReply::finished()
yor slot can be triggered even when a request from a totally unrelated class is finished