Method to reset the QNetworkAccessManager backend

旧街凉风 提交于 2019-12-05 12:41:49

You are connecting the QNetworkReply signal finished() like this:

QObject::connect(reply, SIGNAL(finished()), netty, SLOT(finished()));

Instead try the finished signal from the QNetworkAccessManager like this:

connect(&network, SIGNAL(finished(QNetworkReply *)), netty, SLOT(finished(QNetworkReply *)));

Note that you will have to change the prototype for your finished() slot.

A final method would be to set a timer that calls a function like this:

void check(QNetworkReply *reply){
 if(reply != NULL){
   if(reply->isFinished())
     finished(reply);
   else
     reply->abort();
 }
}

And be aware of this bug:

https://bugreports.qt-project.org/browse/QTBUG-3443

Konstantinos Gaitanis

I'm experiencing the same situation with Qt 5.3.0 (okay its still beta but I strongly believe that the same bug is reproduced on Qt5.2.x).

QNetworkAccessManager correctly reports the fileNotFound error for ftp but does never recover from it. Any subsequent ftp requests fail with the same error message.

Suppose try to fetch badFile.txt (which does not exist). We get the correct message that badFile.txt was not found. If we then try to access goodFile.txt (which exists) we get

Error while downloading ftp://ftp.url.com/goodFile.txt: 
Downloading file failed:
Can't open /badFile.txt: No such file or directory"

The previous error seems to stay stuck in QNetworkAccessManager for ever.

The only solution that worked for me was to delete and create a new QNetworkAccessManager every time a ftp not found error occurs. This is done by simply connecting to the QNetworkReply error(QNetworkReply::NetworkError) signal. As stated by elmigranto, the finished signal will be emitted for each active request. This can obviously only work if the QNetworkAccessManager handles a single request, otherwise special care must be taken not to abort any parallel requests handled by the same manager.

The QNetworkAccessManager finished(QNetworkReply*) and the QNetworkReply finished() signals do exactly the same thing. As stated in the Qt docs these two signals are emitted in tandem. This is NOT a solution for this problem as stated by buster.

Setting up a timer can be useful for other purposes (such as connection timeout) but will not resolve this particular problem either.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!