Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Timeout reply from QNetworkReply
Forum Updated to NodeBB v4.3 + New Features

Timeout reply from QNetworkReply

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 6 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Damian7546
    wrote on 15 Dec 2022, 11:00 last edited by
    #1

    In my code I have function to get request from http like below:

    void NetworkWorker::get(QString location)
    {
        qInfo() << "Getting from server....";
        QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location)));
    
        QVariant statusCode = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
        if ( !statusCode.isValid() )
        return;
    
        int status = statusCode.toInt();
        if ( status != 200 )
        {
            QString reason = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString();
        }
        connect(reply,&QNetworkReply::readyRead,this,&NetworkWorker::readyRead);
    }
    
    void NetworkWorker::readyRead()
    {
        QByteArray data;
        QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
        if (reply) {
            data = reply->readAll();
        }
        networkReply_byRfid(data);
    }
    

    How emit signal when is timeout, without any data to recive ? Is any build in mechanizm in QNetworkReply ?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 15 Dec 2022, 11:52 last edited by
      #2

      You can set a timeout in your QNetworkRequest.
      I expect you get a suitable code in the QNetworkReply::errorOccurred() signal if no data is received inside that timeout.

      If memory serves, the request is not sent until your code makes it to the Qt event loop. The way your code is written, you are checking for HTTP response codes before the GET has been sent.

      1 Reply Last reply
      2
      • D Offline
        D Offline
        Damian7546
        wrote on 15 Dec 2022, 20:08 last edited by Damian7546
        #3

        @ChrisW67 said in Timeout reply from QNetworkReply:

        If memory serves, the request is not sent until your code makes it to the Qt event loop. The way your code is written, you are checking for HTTP response codes before the GET has been sent.

        I corrected it

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 15 Dec 2022, 20:12 last edited by
          #4

          Hi,

          Use the finished signal of QNetworkReply.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          D 1 Reply Last reply 15 Dec 2022, 20:33
          2
          • S SGaist
            15 Dec 2022, 20:12

            Hi,

            Use the finished signal of QNetworkReply.

            D Offline
            D Offline
            Damian7546
            wrote on 15 Dec 2022, 20:33 last edited by
            #5

            @SGaist said in Timeout reply from QNetworkReply:

            Hi,

            Use the finished signal of QNetworkReply.

            Can you clarify ?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 15 Dec 2022, 20:53 last edited by
              #6

              Connect it a slot to it and there you can check whatever you want from the reply. The query will be done at that point.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              J 1 Reply Last reply 15 Dec 2022, 20:55
              0
              • S SGaist
                15 Dec 2022, 20:53

                Connect it a slot to it and there you can check whatever you want from the reply. The query will be done at that point.

                J Offline
                J Offline
                JoeCFD
                wrote on 15 Dec 2022, 20:55 last edited by JoeCFD
                #7

                @SGaist It may not always be the case. If the reply comes instantly, the slot will never be called. Therefore, check the reply first and then connect.

                J 1 Reply Last reply 15 Dec 2022, 21:47
                0
                • J JoeCFD
                  15 Dec 2022, 20:55

                  @SGaist It may not always be the case. If the reply comes instantly, the slot will never be called. Therefore, check the reply first and then connect.

                  J Offline
                  J Offline
                  JonB
                  wrote on 15 Dec 2022, 21:47 last edited by
                  #8

                  @JoeCFD said in Timeout reply from QNetworkReply:

                  If the reply comes instantly, the slot will never be called. Therefore, check the reply first and then connect.

                  ?

                  J 1 Reply Last reply 15 Dec 2022, 22:21
                  0
                  • J JonB
                    15 Dec 2022, 21:47

                    @JoeCFD said in Timeout reply from QNetworkReply:

                    If the reply comes instantly, the slot will never be called. Therefore, check the reply first and then connect.

                    ?

                    J Offline
                    J Offline
                    JoeCFD
                    wrote on 15 Dec 2022, 22:21 last edited by JoeCFD
                    #9

                    @JonB

                    QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location)));
                    if ( nullptr != reply ) {
                        if ( reply->isFinished()) { /* it may have been done already */
                            call slot();
                        }
                        else {
                            connect();
                        }
                    }
                    
                    J D 2 Replies Last reply 15 Dec 2022, 23:32
                    0
                    • J JoeCFD
                      15 Dec 2022, 22:21

                      @JonB

                      QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location)));
                      if ( nullptr != reply ) {
                          if ( reply->isFinished()) { /* it may have been done already */
                              call slot();
                          }
                          else {
                              connect();
                          }
                      }
                      
                      J Offline
                      J Offline
                      JonB
                      wrote on 15 Dec 2022, 23:32 last edited by JonB
                      #10

                      @JoeCFD
                      Have you tested to see whether, with a connect(), the finished signal would still actually be received and trigger the slot (so no need to test isFinished())? My understanding is that is how slot connections work.

                      J 1 Reply Last reply 15 Dec 2022, 23:57
                      0
                      • J JonB
                        15 Dec 2022, 23:32

                        @JoeCFD
                        Have you tested to see whether, with a connect(), the finished signal would still actually be received and trigger the slot (so no need to test isFinished())? My understanding is that is how slot connections work.

                        J Offline
                        J Offline
                        JoeCFD
                        wrote on 15 Dec 2022, 23:57 last edited by
                        #11

                        @JonB I had a case before. Therefore, I know this.

                        1 Reply Last reply
                        1
                        • J JoeCFD
                          15 Dec 2022, 22:21

                          @JonB

                          QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location)));
                          if ( nullptr != reply ) {
                              if ( reply->isFinished()) { /* it may have been done already */
                                  call slot();
                              }
                              else {
                                  connect();
                              }
                          }
                          
                          D Offline
                          D Offline
                          Damian7546
                          wrote on 16 Dec 2022, 07:15 last edited by Damian7546
                          #12

                          @JoeCFD said in Timeout reply from QNetworkReply:

                          @JonB

                          QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location)));
                          if ( nullptr != reply ) {
                              if ( reply->isFinished()) { /* it may have been done already */
                                  call slot();
                              }
                              else {
                                  connect();
                              }
                          }
                          

                          slot() --> it is my NetworkWorker::readyRead() ?

                          connect() --> what connect() are you talking about?

                          Still I do not know when I should emit signal about timeout recived data ....

                          jsulmJ 1 Reply Last reply 16 Dec 2022, 07:18
                          0
                          • D Damian7546
                            16 Dec 2022, 07:15

                            @JoeCFD said in Timeout reply from QNetworkReply:

                            @JonB

                            QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location)));
                            if ( nullptr != reply ) {
                                if ( reply->isFinished()) { /* it may have been done already */
                                    call slot();
                                }
                                else {
                                    connect();
                                }
                            }
                            

                            slot() --> it is my NetworkWorker::readyRead() ?

                            connect() --> what connect() are you talking about?

                            Still I do not know when I should emit signal about timeout recived data ....

                            jsulmJ Online
                            jsulmJ Online
                            jsulm
                            Lifetime Qt Champion
                            wrote on 16 Dec 2022, 07:18 last edited by
                            #13

                            @Damian7546 said in Timeout reply from QNetworkReply:

                            connect() --> what connect() are you talking about?

                            connect(reply,&QNetworkReply::readyRead,this,&NetworkWorker::readyRead);
                            

                            slot() is your NetworkWorker::readyRead.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            1
                            • D Offline
                              D Offline
                              Damian7546
                              wrote on 16 Dec 2022, 08:44 last edited by Damian7546
                              #14

                              And where timeout is expired ?
                              I still understand where I can give for example : qDebug() << "timeout has expired to receive data"

                              void NetworkWorker::get(QString location)
                              {
                                  QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location)));
                              
                                  if ( nullptr != reply ) {
                                      if ( reply->isFinished()) {
                                          readyRead();
                                      }
                                      else {
                                          connect(reply,&QNetworkReply::readyRead,this,&NetworkWorker::readyRead);
                                      }
                                  }
                              }
                              
                              void NetworkWorker::readyRead()
                              {
                                  QByteArray data;
                                  QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
                                  QVariant statusCode = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
                              
                                  if ( !statusCode.isValid() )
                                  return;
                              
                                  int status = statusCode.toInt();
                                  if ( status != 200 )
                                  {
                                      QString reason = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString();
                                  }
                                  else
                                  {
                                      if (reply) {
                                          data = reply->readAll();
                                      }
                                      networkReply(data);
                                  }
                              }
                              
                              jsulmJ 1 Reply Last reply 16 Dec 2022, 09:44
                              0
                              • D Damian7546
                                16 Dec 2022, 08:44

                                And where timeout is expired ?
                                I still understand where I can give for example : qDebug() << "timeout has expired to receive data"

                                void NetworkWorker::get(QString location)
                                {
                                    QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location)));
                                
                                    if ( nullptr != reply ) {
                                        if ( reply->isFinished()) {
                                            readyRead();
                                        }
                                        else {
                                            connect(reply,&QNetworkReply::readyRead,this,&NetworkWorker::readyRead);
                                        }
                                    }
                                }
                                
                                void NetworkWorker::readyRead()
                                {
                                    QByteArray data;
                                    QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
                                    QVariant statusCode = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
                                
                                    if ( !statusCode.isValid() )
                                    return;
                                
                                    int status = statusCode.toInt();
                                    if ( status != 200 )
                                    {
                                        QString reason = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString();
                                    }
                                    else
                                    {
                                        if (reply) {
                                            data = reply->readAll();
                                        }
                                        networkReply(data);
                                    }
                                }
                                
                                jsulmJ Online
                                jsulmJ Online
                                jsulm
                                Lifetime Qt Champion
                                wrote on 16 Dec 2022, 09:44 last edited by
                                #15

                                @Damian7546 Call https://doc.qt.io/qt-6/qnetworkreply.html#error in your readyRead() slot and check it return value.

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                D 1 Reply Last reply 16 Dec 2022, 13:35
                                1
                                • jsulmJ jsulm
                                  16 Dec 2022, 09:44

                                  @Damian7546 Call https://doc.qt.io/qt-6/qnetworkreply.html#error in your readyRead() slot and check it return value.

                                  D Offline
                                  D Offline
                                  Damian7546
                                  wrote on 16 Dec 2022, 13:35 last edited by Damian7546
                                  #16

                                  @jsulm Can you give me an example ?

                                  After QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); I added:
                                  qDebug() << "http error: " << reply->error();
                                  And when I have connection to http server the shows message:
                                  http error: QNetworkReply::NoError

                                  But nothing shows up when there is no connection, because readyread doesn't appear.

                                  I should in this function check timeout:

                                  void NetworkWorker::get(QString location)
                                  {
                                      qInfo() << "Getting from server....";
                                      QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location)));
                                  
                                  
                                      connect(reply,&QNetworkReply::readyRead,this,&NetworkWorker::readyRead);
                                  
                                  }
                                  
                                  

                                  So in abov funnction added :
                                  connect(reply,&QNetworkReply::errorOccurred,this,&NetworkWorker::errorOccurred);

                                  but never occurred....

                                  1 Reply Last reply
                                  0

                                  1/16

                                  15 Dec 2022, 11:00

                                  • Login

                                  • Login or register to search.
                                  1 out of 16
                                  • First post
                                    1/16
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved