在Qt中处理网络超时

por*_*rsh 4 qt qnetworkaccessmanager

处理时QNetworkReply,规定使用计时器中止连接.

这是我目前的代码:

void ImageDownloader::download(QString imgUrl){    
  this->timeoutTimer = new QTimer(this);
  this->timeoutTimer->setSingleShot(true);
  this->timeoutTimer->setInterval(15000);
  connect(this->timeoutTimer, SIGNAL(timeout()), this, SLOT(timeout()));

  QUrl requestUrl(imgUrl);
  QNetworkRequest nwRequest(requestUrl);
  this->imageNwReply = this->nam->get(nwRequest);
  connect(imageNwReply,SIGNAL(finished()),this,SLOT(imgDownloaded()));
  connect(imageNwReply, SIGNAL(downloadProgress(qint64,qint64)), this->timeoutTimer, SLOT(start()));
  this->timeoutTimer->start();
}

void ImageDownloader::timeout(){
  qDebug()<<__FUNCTION__<<" Forced timeout!";    
  this->imageNwReply->abort();
}
Run Code Online (Sandbox Code Playgroud)

我面临的困惑是什么时候应该启动计时器?有时我必须从大约50个并发Get请求中进行,QNetworkAccessManager但由于存在最大并发连接的限制,有时会发生某些请求甚至在处理之前就超时.

是否有信号确切地知道何时开始处理请求,QNeworkAccessManager以便我只能启动相应的计时器?

一种可能的解决方案可能是实现一个请求队列,并且只有最大可能的连接来处理,但我正在寻找一个更清洁的解决方案

por*_*rsh 5

该问题有一个开放的错误/增强请求.我从Qt论坛了解了这个bug

  • 第三年通过这样一个基本功能:| 我对此抱怨不已.这是一个迹象,我应该实现一个补丁并发布它.. (4认同)