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. QNetworkAccessManager signals not working
Qt 6.11 is out! See what's new in the release blog

QNetworkAccessManager signals not working

Scheduled Pinned Locked Moved General and Desktop
14 Posts 4 Posters 8.5k Views 3 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
    Defohin
    wrote on last edited by Defohin
    #1

    I don't know why but it doesn't work at all, not the finished, error, or sslErrors, none of them:

    auto manager = new QNetworkAccessManager;
    
    QNetworkRequest request;
    request.setUrl(QUrl("http://qt-project.org"));
    request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
    
    QNetworkReply *reply = manager->get(request);
    connect(reply, SIGNAL(readyRead()), this, SLOT(replyReadyRead()));
    connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
                    this, SLOT(replyError(QNetworkReply::NetworkError)));
    connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
                    this, SLOT(replySslErrors(QList<QSslError>)));
    

    I even tried to use the new syntax like connect(networkReply, static_cast<void(QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error), [=](QNetworkReply::NetworkError code){ /* ... */ }); but doesn't work either...
    What is happening?

    raven-worxR 1 Reply Last reply
    0
    • D Defohin

      I don't know why but it doesn't work at all, not the finished, error, or sslErrors, none of them:

      auto manager = new QNetworkAccessManager;
      
      QNetworkRequest request;
      request.setUrl(QUrl("http://qt-project.org"));
      request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
      
      QNetworkReply *reply = manager->get(request);
      connect(reply, SIGNAL(readyRead()), this, SLOT(replyReadyRead()));
      connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
                      this, SLOT(replyError(QNetworkReply::NetworkError)));
      connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
                      this, SLOT(replySslErrors(QList<QSslError>)));
      

      I even tried to use the new syntax like connect(networkReply, static_cast<void(QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error), [=](QNetworkReply::NetworkError code){ /* ... */ }); but doesn't work either...
      What is happening?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Defohin

      • check the return value of connect()
      • check the console for warnings regarding the connect() statements
      • show some more code, what happen with the reply/QNetworkAccessManager?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • D Offline
        D Offline
        Defohin
        wrote on last edited by Defohin
        #3

        @raven-worx

        1 - All of them is returning true
        2 - There's nothing on the console
        3 - I just realized that I forgot to connect finished ...

        auto manager = new QNetworkAccessManager;
        
        QNetworkRequest request;
        request.setUrl(QUrl("http://qt-project.org"));
        request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
        
        QNetworkReply *reply = manager->get(request);
        auto r1 = connect(reply, SIGNAL(finished()), this, SLOT(replyFinished()));
        auto r2 = connect(reply, SIGNAL(readyRead()), this, SLOT(replyReadyRead()));
        auto r3 = connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
                this, SLOT(replyError(QNetworkReply::NetworkError)));
        auto r4 = connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
                this, SLOT(replySslErrors(QList<QSslError>)));
        
        qDebug() << r1 << r2 << r3 << r4;
        

        The rest of the code is the slots, but it has nothing special:

        void NYTimes::replyFinished()
        {
            qDebug() << "it is working";
        }
        
        void NYTimes::replyReadyRead()
        {
            qDebug() << "ready to read";
        }
        
        void NYTimes::replyError(QNetworkReply::NetworkError error)
        {
            qDebug() << error;
        }
        
        void NYTimes::replySslErrors(QList<QSslError> errors)
        {
            qDebug() << errors;
        }
        

        And yes, it's not working either.

        raven-worxR 1 Reply Last reply
        0
        • D Defohin

          @raven-worx

          1 - All of them is returning true
          2 - There's nothing on the console
          3 - I just realized that I forgot to connect finished ...

          auto manager = new QNetworkAccessManager;
          
          QNetworkRequest request;
          request.setUrl(QUrl("http://qt-project.org"));
          request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
          
          QNetworkReply *reply = manager->get(request);
          auto r1 = connect(reply, SIGNAL(finished()), this, SLOT(replyFinished()));
          auto r2 = connect(reply, SIGNAL(readyRead()), this, SLOT(replyReadyRead()));
          auto r3 = connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
                  this, SLOT(replyError(QNetworkReply::NetworkError)));
          auto r4 = connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
                  this, SLOT(replySslErrors(QList<QSslError>)));
          
          qDebug() << r1 << r2 << r3 << r4;
          

          The rest of the code is the slots, but it has nothing special:

          void NYTimes::replyFinished()
          {
              qDebug() << "it is working";
          }
          
          void NYTimes::replyReadyRead()
          {
              qDebug() << "ready to read";
          }
          
          void NYTimes::replyError(QNetworkReply::NetworkError error)
          {
              qDebug() << error;
          }
          
          void NYTimes::replySslErrors(QList<QSslError> errors)
          {
              qDebug() << errors;
          }
          

          And yes, it's not working either.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @Defohin
          if all connects are successfull (by returning true), the only thing i can think of is that the reply (or QNAM) or the object holding the slots are deleted?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1
          • D Offline
            D Offline
            Defohin
            wrote on last edited by
            #5

            It's not being deleted, I'm using pointers to test if it has something to do with that, but it doesn't work either.

            1 Reply Last reply
            0
            • m.sueM Offline
              m.sueM Offline
              m.sue
              wrote on last edited by
              #6

              Hi,
              can you try with a site that does not redirect to https e.g. http://en.cppreference.com/w/. Maybe that's a problem.
              -Michael.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Defohin
                wrote on last edited by
                #7

                It doesn't work either, I really don't know what is going on.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Defohin
                  wrote on last edited by Defohin
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • m.sueM Offline
                    m.sueM Offline
                    m.sue
                    wrote on last edited by m.sue
                    #9

                    Hi,
                    please try to make QNetworkReply *reply a class member variable. As you have it now it gets lost as soon as you leave the function. You will need it anyway in the readyReadslot.
                    -Michael.

                    1 Reply Last reply
                    1
                    • D Offline
                      D Offline
                      Defohin
                      wrote on last edited by
                      #10

                      The problem was on the instance of the class, I had to use it as a pointer.

                      auto nytimes = new NYTimes;
                      nytimes->parse();
                      

                      instead of:

                      NYTimes nytimes;
                      nytimes.parse();
                      

                      Is there a reason for that?

                      mrjjM 1 Reply Last reply
                      0
                      • D Defohin

                        The problem was on the instance of the class, I had to use it as a pointer.

                        auto nytimes = new NYTimes;
                        nytimes->parse();
                        

                        instead of:

                        NYTimes nytimes;
                        nytimes.parse();
                        

                        Is there a reason for that?

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Hi
                        It depends on the context

                        void Func() {
                        auto nytimes = new NYTimes;
                        nytimes->parse();
                        } // after here NYTimes still lives

                        void Func() {
                        NYTimes nytimes;
                        nytimes.parse();
                        } // after here NYTimes is no more

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          Defohin
                          wrote on last edited by
                          #12

                          I was calling NYTimes nytimes; on the constructor of the QMainWindow

                          mrjjM 1 Reply Last reply
                          0
                          • D Defohin

                            I was calling NYTimes nytimes; on the constructor of the QMainWindow

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @Defohin said in QNetworkAccessManager signals not working:

                            NYTimes nytimes; on the constructor of the QMainWindow

                            So it means that at the time when
                            MainWindow was shown, nytimes would have been deleted.
                            Its called "running out of scope". Applies to all variables on the stack.

                            1 Reply Last reply
                            1
                            • D Offline
                              D Offline
                              Defohin
                              wrote on last edited by
                              #14

                              Thank you, it's solved

                              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