QReplyTimeout.h 733 B

12345678910111213141516171819202122232425262728293031
  1. #ifndef QREPLYTIMEOUT_H
  2. #define QREPLYTIMEOUT_H
  3. #include <QObject>
  4. #include <QTimer>
  5. #include <QNetworkReply>
  6. #include <stdio.h>
  7. class QReplayTimeout : public QObject {
  8. Q_OBJECT
  9. public:
  10. QReplayTimeout(QNetworkReply *reply, const int timeout) :QObject(reply) {
  11. Q_ASSERT(reply);
  12. if( reply && reply->isRunning()){
  13. QTimer::singleShot(timeout,this,SLOT(onTimeout()));
  14. }
  15. }
  16. signals:
  17. void net_timeout();
  18. private slots:
  19. void onTimeout(){
  20. QNetworkReply *reply = static_cast<QNetworkReply *>(parent());
  21. if(reply->isRunning()){
  22. reply->abort();
  23. reply->deleteLater();
  24. emit net_timeout();
  25. }
  26. }
  27. };
  28. #endif // QREPLYTIMEOUT_H