PDA

View Full Version : Tracking multiple requests with QNetwork



rbp
19th February 2009, 07:52
hello,

I have a class that communicates with a REST interface using the QNetwork stack.
Here is some example code to make a HTTP request to the server that calls 'serverRequest()' when finished and 'requestError()' on error:



QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(serverRequest(QNetworkReply *)));
QNetworkReply *reply = manager->get(QNetworkRequest(url));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(requestError(QNetworkReply::NetworkError)));


void serverRequest(QNetworkReply *r) {
if(r->error() == QNetworkReply::NoError) {
...
}
delete r->manager();
}

void requestError(QNetworkReply::NetworkError e) {
switch(e) {
case QNetworkReply::ConnectionRefusedError:
...
}
}


My problem is that for certain errors (such as timeout but not 404) the error() signal is emitted but not finished().
The error signal only passes an integer of the type of error so there is no direct way to tell which request failed. Also I can't clean up the memory used by the request.

Currently I get around this with a QMap that stores when a request is made with a pointer to the QNetworkRequest instance. Then I use a QTimer to periodically retry unfinished requests.
But this feels like a big hack. Is there a more elegant and flexible way to make network requests reliably?

Richard

rbp
17th March 2009, 02:29
does anyone know how to handle multiple network requests robustly with Qt?

ehovaere
31st May 2010, 15:55
Hey,

it's probably too late and I hope you found the answer by yourself, but for information, I think this->sender() will do the job.

rbp
1st June 2010, 01:32
that project wrapped up some time ago, but thanks for the reference in future

squidge
1st June 2010, 09:04
Rather than sender() and ugly type casting, your better off with QSignalMapper