How to properly cancel QNetworkAccessManager request?
-
In application I need sometimes stop working QNetworkAccessManager to prevent it emit finished() signal and free network resources. Of course it is possible just temporary disconnect this signal but it is not useful in current code. There are several managers in application and I have to track which one was disconnected to reconnect it again when needed. And signal disconnection won't free resources and close open connections. I'd better just send a signal or something like QNetworkAccessManager::cancel(); but I do not see this function. I do not even see how to do this with QNetworkRequest or QNetworkSession. Is there any way? Anybody help?
-
Looks like solved by myself...
@ QNetworkSession session( manager->configuration() );
session.stop();@is that correct?
-
You should abort the active QNetworkReply using the abort() slot.
http://doc-snapshot.qt-project.org/5.0/qnetworkreply.html#abort
Using QNetworkSession::stop() will disconnect you from the internet, if allowed. (which resets all connections).
-
Does this mean? - I have to save QNetworkReplay* reply; returned by QNetworkAccessManager::get() and call replay->abort() for this reply when needed
stop() will disconnect only for one QNetworkAccessManager or for all managers? Exactly I need disconnect one manager from it's connections.
BTW: I use Qt 4.7
-
You maybe using its readyRead and finished signals already?
abort() is a slot, so you could just connect a signal from your own class to the object.e.g.
QNetworkReply *nr = manager.get(request);
QObject::connect(this, SIGNAL(cancelDownload()), nr, SLOT(abort()));And then when you want to cancel downloads:
emit cancelDownload();QNetworkSession is for handling the low level network adaptor.
stop() is equivalent to unplugging the network cable. -
bq. You maybe using its readyRead and finished signals already?
no, just only finished()
bq. stop() is equivalent to unplugging the network cable
I did not find such a behavior... Why it should turn off Inet connection if it is called for QNetwork Session but not for "connection"?
-
http://qt-project.org/doc/qt-4.7/qnetworksession.html states:
"Invalidates all open sessions against the network interface and therefore stops the underlying network interface. This function always changes the session's state() flag to Disconnected."(emphasis mine)
-
I read this but understood "underlying network interface" not as "network adapter system interface" but as "Qt network interface lying under QNetworkSession" instead. I cannot believe that application can so easy stop all network connections for entire system. I would expect that system-level functions were described with specific warnings. Member stop() would be described clearly as "This will stop all your network connections on entire system". I do not see it stops any one but open in application.
Can anybody else familiar with QNetworkSession class comment this?