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.3k 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 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 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 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
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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
          2
          • SGaistS SGaist

            Hi,

            Use the finished signal of QNetworkReply.

            D Offline
            D Offline
            Damian7546
            wrote on 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
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 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

              JoeCFDJ 1 Reply Last reply
              0
              • SGaistS SGaist

                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.

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on 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.

                JonBJ 1 Reply Last reply
                0
                • JoeCFDJ JoeCFD

                  @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.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on 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.

                  ?

                  JoeCFDJ 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @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.

                    ?

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on 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();
                        }
                    }
                    
                    JonBJ D 2 Replies Last reply
                    0
                    • JoeCFDJ JoeCFD

                      @JonB

                      QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location)));
                      if ( nullptr != reply ) {
                          if ( reply->isFinished()) { /* it may have been done already */
                              call slot();
                          }
                          else {
                              connect();
                          }
                      }
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on 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.

                      JoeCFDJ 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @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.

                        JoeCFDJ Offline
                        JoeCFDJ Offline
                        JoeCFD
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        1
                        • JoeCFDJ JoeCFD

                          @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 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
                          0
                          • D Damian7546

                            @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 Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 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 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
                              0
                              • D Damian7546

                                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 Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on 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
                                1
                                • jsulmJ jsulm

                                  @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 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

                                  • Login

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