处理Qt中的网络超时

问题描述:

当处理QNetworkReply时,使用定时器中止连接是prescribed处理Qt中的网络超时

这里是我当前的代码:

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(); 
} 

我所面临的困惑是,当我应该开始计时?有时我必须做出大约50个并发获取来自QNetworkAccessManager的请求,但由于存在throttling for maximum concurrent connections,有时甚至在处理之前,有些请求会超时。

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

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

有一个开放的bug/enhancement request的问题。我从Qt forum

+4

了解了这个bug。第三年通过了这样一个基本功能:我对此抱怨很大。这表明我应该实施一个补丁并发布它.. – quetzalcoatl 2012-10-09 16:39:13