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