Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Slot doesn't get triggered on HTTP reply
Qt 6.11 is out! See what's new in the release blog

Slot doesn't get triggered on HTTP reply

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 5 Posters 4.8k 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.
  • R rackover

    Hi,
    I'm trying to fetch JSON data from a web page using QNetworkRequest, but the slot never gets triggered despite the URL being valid and working. I've tried with any URL and it does not work.
    Heres my code :

    ///header file
    #ifndef DOWNLOADER_H
    #define DOWNLOADER_H
    
    #include <QObject>
    #include <QJsonObject>
    #include <QNetworkReply>
    
    #include <mutex>
    #include <condition_variable>
    
    namespace Pico{
        namespace Downloader{
    
            class Funcs : public QObject
            {
                Q_OBJECT
                public:
                    explicit Funcs(QObject *parent = 0);
                    void DownloadFiles(std::map<std::string, int> &downloadsMap);
                    void PopulateDownloadsMap(QJsonObject game, std::map<std::string, int> downloadsMap);
    
                    struct currentDownload{
                        std::condition_variable    finished;
                        int                     state;
                        std::string              details;
                        QString                   data;
                        std::mutex               lock;
                    };
    
                signals:
    
                public slots:
                    void onAPIResponse(QNetworkReply*);
    
                private:
    
            };
        }
    }
    
    #endif // DOWNLOADER_H
    
    
    
                   //function to start the download
    
                   std::string modApiPage = "https://api.faforever.com/data/mod?page[size]=1&filter=versions.uid=="+uid+"&include=versions&fields[modVersion]=downloadUrl";
    
                    /// Fetch download address from API using mod UID
                    const QUrl url = QUrl(QString::fromStdString(modApiPage));
                    const QNetworkRequest request(url);
                    QNetworkAccessManager networkManager;
                    connect(&networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onAPIResponse(QNetworkReply*))) ;
    
    
     void Funcs::onAPIResponse(QNetworkReply* reply)
            {
                logging.Write("ON_VAULT_RESPONSE => Received data from the API");
                ...further operations...
            }
    

    Am I doing something wrong ? I"ve tried following multiple tutorials but I can"t get this to work.

    Paul ColbyP Offline
    Paul ColbyP Offline
    Paul Colby
    wrote on last edited by Paul Colby
    #2

    Hi @rackover,

    Where you have:

    QNetworkAccessManager networkManager;
    connect(&networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onAPIResponse(QNetworkReply*))) ;
    

    The signal is disconnected and networkManager destroyed immediately after the function ends - ie when accessManager goes out of scope. Possibly this is just because you simplified the code to show us the sample, but you should either be allocating networkManager outside this function, or allocating it on a heap.

    eg:

    QNetworkAccessManager * networkManager = new QNetworkAccessManager(this);
    connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onAPIResponse(QNetworkReply*))) ;
    

    Cheers.

    1 Reply Last reply
    4
    • R rackover

      Hi,
      I'm trying to fetch JSON data from a web page using QNetworkRequest, but the slot never gets triggered despite the URL being valid and working. I've tried with any URL and it does not work.
      Heres my code :

      ///header file
      #ifndef DOWNLOADER_H
      #define DOWNLOADER_H
      
      #include <QObject>
      #include <QJsonObject>
      #include <QNetworkReply>
      
      #include <mutex>
      #include <condition_variable>
      
      namespace Pico{
          namespace Downloader{
      
              class Funcs : public QObject
              {
                  Q_OBJECT
                  public:
                      explicit Funcs(QObject *parent = 0);
                      void DownloadFiles(std::map<std::string, int> &downloadsMap);
                      void PopulateDownloadsMap(QJsonObject game, std::map<std::string, int> downloadsMap);
      
                      struct currentDownload{
                          std::condition_variable    finished;
                          int                     state;
                          std::string              details;
                          QString                   data;
                          std::mutex               lock;
                      };
      
                  signals:
      
                  public slots:
                      void onAPIResponse(QNetworkReply*);
      
                  private:
      
              };
          }
      }
      
      #endif // DOWNLOADER_H
      
      
      
                     //function to start the download
      
                     std::string modApiPage = "https://api.faforever.com/data/mod?page[size]=1&filter=versions.uid=="+uid+"&include=versions&fields[modVersion]=downloadUrl";
      
                      /// Fetch download address from API using mod UID
                      const QUrl url = QUrl(QString::fromStdString(modApiPage));
                      const QNetworkRequest request(url);
                      QNetworkAccessManager networkManager;
                      connect(&networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onAPIResponse(QNetworkReply*))) ;
      
      
       void Funcs::onAPIResponse(QNetworkReply* reply)
              {
                  logging.Write("ON_VAULT_RESPONSE => Received data from the API");
                  ...further operations...
              }
      

      Am I doing something wrong ? I"ve tried following multiple tutorials but I can"t get this to work.

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

      @rackover said in Slot doesn't get triggered on HTTP reply:

      QNetworkAccessManager networkManager;

      you create the QNetworkAccessManager isnatnce on the stack, which means it gets automatically deleted on the end of the block it is created in.
      When this is not intentional, this is your issue. Create it on the heap (pointer) and set a parent object on it and it should work.

      Edit: too slow :)

      --- 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
      4
      • R Offline
        R Offline
        rackover
        wrote on last edited by
        #4

        @Paul-Colby said in Slot doesn't get triggered on HTTP reply:

        QNetworkAccessManager * networkManager = new QNetworkAccessManager(this);
        connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onAPIResponse(QNetworkReply*))) ;

        Hi,
        Thanks from the help from you both. I've tried correcting the code, but the signal still doesn't get triggered.

                       std::string modApiPage = "https://api.faforever.com/data/mod?page[size]=1&filter=versions.uid=="+uid+"&include=versions&fields[modVersion]=downloadUrl";
        
                        /// Fetch download address from API using mod UID
                        const QUrl url = QUrl(QString::fromStdString(modApiPage));
                        const QNetworkRequest request(url);
                        QNetworkAccessManager * networkManager = new QNetworkAccessManager(this);
                        connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onAPIResponse(QNetworkReply*))) ;
        
        jsulmJ 1 Reply Last reply
        0
        • R rackover

          @Paul-Colby said in Slot doesn't get triggered on HTTP reply:

          QNetworkAccessManager * networkManager = new QNetworkAccessManager(this);
          connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onAPIResponse(QNetworkReply*))) ;

          Hi,
          Thanks from the help from you both. I've tried correcting the code, but the signal still doesn't get triggered.

                         std::string modApiPage = "https://api.faforever.com/data/mod?page[size]=1&filter=versions.uid=="+uid+"&include=versions&fields[modVersion]=downloadUrl";
          
                          /// Fetch download address from API using mod UID
                          const QUrl url = QUrl(QString::fromStdString(modApiPage));
                          const QNetworkRequest request(url);
                          QNetworkAccessManager * networkManager = new QNetworkAccessManager(this);
                          connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onAPIResponse(QNetworkReply*))) ;
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #5

          @rackover said in Slot doesn't get triggered on HTTP reply:

          QNetworkAccessManager * networkManager = new QNetworkAccessManager(this);

          Is this the one QNetworkAccessManager you're using for request?
          Also check whether connect() actually succeeded.

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

          1 Reply Last reply
          1
          • R Offline
            R Offline
            rackover
            wrote on last edited by
            #6

            Yes, I believe this is the networkmanager i'm using. How can I check that ?
            Connect doesn't return a bool nor a int, I'm not sure of how I should check it's working or not. At least it's not returning any errors.

            jsulmJ 1 Reply Last reply
            0
            • R rackover

              Yes, I believe this is the networkmanager i'm using. How can I check that ?
              Connect doesn't return a bool nor a int, I'm not sure of how I should check it's working or not. At least it's not returning any errors.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @rackover If the old style connect fails you will see a warning at runtime (in application output tab in QtCreator).

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

              1 Reply Last reply
              0
              • R Offline
                R Offline
                rackover
                wrote on last edited by rackover
                #8

                Then I guess it is not failing. There is no warning neither at runtime nor in IDE. Is there any other possibility I can explore ?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  Hi,

                  You should also connect the error related signal.

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

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    rackover
                    wrote on last edited by
                    #10

                    Hi,

                    Thank you for your help, could you please expand a little bit ? I'm not sure of what you mean by connecting the "error related" signal. Isn't it what I'm actually doing ?

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      You only connect the finished signal. There's also sslErrors.

                      Do you also have OpenSSL properly installed on your system ?

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

                      1 Reply Last reply
                      3
                      • R Offline
                        R Offline
                        rackover
                        wrote on last edited by
                        #12

                        I don't, but since it is not working with a classic HTTP address neither, I didn't look into SSL-stuff yet.

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          Then connect the error signal of the QNetworkReply you get back when doing the request.

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

                          1 Reply Last reply
                          1
                          • R Offline
                            R Offline
                            rackover
                            wrote on last edited by
                            #14

                            I get no Network reply back when I'm doing the request. The whole point of this topic is that, the slot getting never triggered, the function that handles the QNetworkReply doesn't fire and therefore, I have no NetworkReply to work with nor to debug.

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by SGaist
                              #15

                              Might be a silly question but since I don't see it in any of it your code snippet, are you calling QNetworkAccessManager::get at some point ?

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

                              1 Reply Last reply
                              3
                              • R Offline
                                R Offline
                                rackover
                                wrote on last edited by
                                #16

                                Ah, yeah, well it surely wasn't going to work without a get. How did I miss that.
                                Thank you.

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