小编por*_*rsh的帖子

在Qt中处理网络超时

处理时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以便我只能启动相应的计时器?

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

qt qnetworkaccessmanager

4
推荐指数
1
解决办法
5214
查看次数

标签 统计

qnetworkaccessmanager ×1

qt ×1