class ReplyTimeout : public QObject {
Q_OBJECT
QBasicTimer m_timer;
public:
QReplyTimeout(QNetworkReply* reply, const int timeout) : QObject(reply) {
Q_ASSERT(reply);
if (reply && reply->isRunning())
m_timer.start(timeout, this);
}
static void set(QNetworkReply* reply, const int timeout) {
new ReplyTimeout(reply, timeout);
}
protected:
void timerEvent(QTimerEvent * ev) {
if (!m_timer.isActive() || ev->timerId() != m_timer.timerId())
return;
auto reply = static_cast<QNetworkReply*>(parent());
if (reply->isRunning())
reply->close();
m_timer.stop();
}
};
Use:
QNetworkAccessManager networkAccessManger;
QNetworkReply* reply =
networkAccessManger.get(QNetworkRequest(QUrl("https://www.google.com")));
ReplyTimeout::set(reply, 100);