Skip to content

Instantly share code, notes, and snippets.

@bigdragon1977
Created May 11, 2016 13:46
Show Gist options
  • Select an option

  • Save bigdragon1977/eb32f6b087b96a9f918765c50c2e3dd9 to your computer and use it in GitHub Desktop.

Select an option

Save bigdragon1977/eb32f6b087b96a9f918765c50c2e3dd9 to your computer and use it in GitHub Desktop.
QNetworkReply and QNetworkAccessManager timeout in http request
QTimer timer;
timer.setSingleShot(true);
QEventLoop loop;
connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
timer.start(30000); // 30 secs. timeout
loop.exec();
if(timer.isActive()) {
timer.stop();
if(m_reply->error() > 0) {
//... // handle error
}
else {
int v = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (v >= 200 && v < 300) { // Success
// ...
}
}
} else {
// timeout
disconnect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
reply->abort();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment