2012-11-20 72 views
0

Im每隔X秒建立一次連接。但是如果淨帶寬超載,定時器在QNetworkAccessManager發送完成信號和應用程序崩潰之前觸發。Qtimer在QNetworkAccessManager完成之前觸發

主窗口::構建體:

pTimer = new QTimer(this); 
connect(pTimer, SIGNAL(timeout()), this, SLOT(connect())); 
pTimer->start(5000); 

的MainWindow ::連接()

pNetworkManager = new QNetworkAccessManager(this); 
connect(pNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(result(QNetworkReply*))); 
pNetworkManager->get(QNetworkRequest(url)); 

的MainWindow ::結果(QNetworkReply *回覆)處理響應

如何檢查QNetworkAccessManager在定時器再次啓動之前完成?

回答

3

不要爲每個請求創建網絡管理器,但只能爲您的類創建一個網絡管理器。否則,每次請求都會泄漏管理員,直到主窗口被銷燬。

QNetworkManager :: get返回指向表示請求的QNetworkReply的指針。您可以將該答覆存儲在QPointer中,連接到它的信號,檢查QNetworkReply :: isFinished()等,以跟蹤請求是否仍在運行。

1

爲什麼不直接在請求完成後啓動計時器?

pTimer = new QTimer(this); 
pTimer->setSingleshot(true); 
connect(pTimer, SIGNAL(timeout()), this, SLOT(connect())); 
pTimer->start(5000); 

pNetworkManager = new QNetworkAccessManager(this); 
connect(pNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(result(QNetworkReply*))); 
connect(pNetworkManager, SIGNAL(finished(QNetworkReply*)), pTimer, SLOT(start())); 
相關問題