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. QNetwork doesn't work

QNetwork doesn't work

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 3 Posters 2.0k Views
  • 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.
  • E Offline
    E Offline
    El3ctroGh0st
    wrote on last edited by
    #1

    Hello,

    I have this piece of code:

    void WeatherReader::getWeatherInfo(QString city)
    {
        qDebug() << "Checkpoint 1";
        QNetworkRequest qRequest(QUrl("https://www.google.com/"));
        QNetworkAccessManager *qManag = new QNetworkAccessManager(this);
        qDebug() << "Checkpoint 2";
    
        connect(qManag, &QNetworkAccessManager::finished, this, &WeatherReader::parseWeatherHTML);
        qManag->get(qRequest);
        qDebug() << "Checkpoint 3";
    }
    
    QStringList WeatherReader::parseWeatherHTML()
    {
        qDebug() << "Checkpoint 4";
    }
    

    The program starts without problems, however, for some reason it never gets to Checkpoint 4. I have added network to the -pro file and lieay32.dll as well as ssleay32.dll to the output directory. It still doesn't seem to work. Any ideas on what might be wrong?

    JonBJ 1 Reply Last reply
    0
    • E El3ctroGh0st

      Hello,

      I have this piece of code:

      void WeatherReader::getWeatherInfo(QString city)
      {
          qDebug() << "Checkpoint 1";
          QNetworkRequest qRequest(QUrl("https://www.google.com/"));
          QNetworkAccessManager *qManag = new QNetworkAccessManager(this);
          qDebug() << "Checkpoint 2";
      
          connect(qManag, &QNetworkAccessManager::finished, this, &WeatherReader::parseWeatherHTML);
          qManag->get(qRequest);
          qDebug() << "Checkpoint 3";
      }
      
      QStringList WeatherReader::parseWeatherHTML()
      {
          qDebug() << "Checkpoint 4";
      }
      

      The program starts without problems, however, for some reason it never gets to Checkpoint 4. I have added network to the -pro file and lieay32.dll as well as ssleay32.dll to the output directory. It still doesn't seem to work. Any ideas on what might be wrong?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @El3ctroGh0st
      Of course it works! :)
      What do you do after Checkpoint #3/exiting getWeatherInfo()? The get() is asynchronous. You need to run the main event loop to get the signal, like QApplication::exec() or equivalent?
      You ought also hook up error signal in case.

      E 1 Reply Last reply
      1
      • JonBJ JonB

        @El3ctroGh0st
        Of course it works! :)
        What do you do after Checkpoint #3/exiting getWeatherInfo()? The get() is asynchronous. You need to run the main event loop to get the signal, like QApplication::exec() or equivalent?
        You ought also hook up error signal in case.

        E Offline
        E Offline
        El3ctroGh0st
        wrote on last edited by
        #3

        @JonB Shouldn't connect(qManag, &QNetworkAccessManager::finished, this, &WeatherReader::parseWeatherHTML); this line be enough? Basically, once it's finished downloading it goes to the function parseWeatherHTML and print "Checkpoint 4", no? But yes, I probably should add a case for errors as well. Will do that.

        JonBJ 1 Reply Last reply
        0
        • E El3ctroGh0st

          @JonB Shouldn't connect(qManag, &QNetworkAccessManager::finished, this, &WeatherReader::parseWeatherHTML); this line be enough? Basically, once it's finished downloading it goes to the function parseWeatherHTML and print "Checkpoint 4", no? But yes, I probably should add a case for errors as well. Will do that.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @El3ctroGh0st
          No, not really, When you connect signal to slot, you must allow event loop to run to call the slot, otherwise nothing happens. You need to look at your code after calling getWeatherInfo(), trust me :)

          E 1 Reply Last reply
          0
          • S Offline
            S Offline
            shaan7
            wrote on last edited by
            #5

            @JonB why are you assuming he is not running an event loop?

            JonBJ 1 Reply Last reply
            0
            • S shaan7

              @JonB why are you assuming he is not running an event loop?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @shaan7
              I am not assuming he is not running an event loop, you can see what I've written, all I have said is: is he running an event loop? And suggested an error slot also just in case.

              1 Reply Last reply
              1
              • JonBJ JonB

                @El3ctroGh0st
                No, not really, When you connect signal to slot, you must allow event loop to run to call the slot, otherwise nothing happens. You need to look at your code after calling getWeatherInfo(), trust me :)

                E Offline
                E Offline
                El3ctroGh0st
                wrote on last edited by
                #7

                @JonB I'm not quite sure what exactly I'm supposed to add, though. :( This is literally all I have:

                WeatherGetter::WeatherGetter(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::WeatherGetter)
                {
                    ui->setupUi(this);
                
                    WeatherReader wReader;
                    wReader.getWeatherInfo("Paris");
                }
                

                Obviously I'll add things later on, but not right now.

                JonBJ S 2 Replies Last reply
                0
                • S Offline
                  S Offline
                  shaan7
                  wrote on last edited by
                  #8

                  @El3ctroGh0st can you paste your main.cpp?

                  E 1 Reply Last reply
                  0
                  • S shaan7

                    @El3ctroGh0st can you paste your main.cpp?

                    E Offline
                    E Offline
                    El3ctroGh0st
                    wrote on last edited by
                    #9

                    @shaan7

                    #include "weathergetter.hpp"
                    #include <QApplication>
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication a(argc, argv);
                        WeatherGetter w;
                        w.show();
                    
                        return a.exec();
                    }
                    
                    1 Reply Last reply
                    0
                    • E El3ctroGh0st

                      @JonB I'm not quite sure what exactly I'm supposed to add, though. :( This is literally all I have:

                      WeatherGetter::WeatherGetter(QWidget *parent) :
                          QMainWindow(parent),
                          ui(new Ui::WeatherGetter)
                      {
                          ui->setupUi(this);
                      
                          WeatherReader wReader;
                          wReader.getWeatherInfo("Paris");
                      }
                      

                      Obviously I'll add things later on, but not right now.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @El3ctroGh0st
                      Your WeatherReader wReader goes out of scope at end of WeatherGetter(), presumably taking the slot with it?

                      1 Reply Last reply
                      3
                      • E El3ctroGh0st

                        @JonB I'm not quite sure what exactly I'm supposed to add, though. :( This is literally all I have:

                        WeatherGetter::WeatherGetter(QWidget *parent) :
                            QMainWindow(parent),
                            ui(new Ui::WeatherGetter)
                        {
                            ui->setupUi(this);
                        
                            WeatherReader wReader;
                            wReader.getWeatherInfo("Paris");
                        }
                        

                        Obviously I'll add things later on, but not right now.

                        S Offline
                        S Offline
                        shaan7
                        wrote on last edited by
                        #11

                        @El3ctroGh0st @JonB is right, you should do this instead-

                        WeatherGetter::WeatherGetter(QWidget *parent) :
                            QMainWindow(parent),
                            ui(new Ui::WeatherGetter)
                        {
                            ui->setupUi(this);
                        
                            WeatherReader *wReader = new WeatherReader(this);
                            wReader->getWeatherInfo("Paris");
                        }
                        

                        or add a mwReader as a private member variable of the WeatherGetter class and use that.

                        E 1 Reply Last reply
                        5
                        • S shaan7

                          @El3ctroGh0st @JonB is right, you should do this instead-

                          WeatherGetter::WeatherGetter(QWidget *parent) :
                              QMainWindow(parent),
                              ui(new Ui::WeatherGetter)
                          {
                              ui->setupUi(this);
                          
                              WeatherReader *wReader = new WeatherReader(this);
                              wReader->getWeatherInfo("Paris");
                          }
                          

                          or add a mwReader as a private member variable of the WeatherGetter class and use that.

                          E Offline
                          E Offline
                          El3ctroGh0st
                          wrote on last edited by
                          #12

                          @shaan7 Ahh yes, now it seems to reach the checkpoint. Thanks! I'm wondering though, what is the difference here between making it a pointer and now? Does making it a pointer prevent it from being deleted after going out of scope?

                          S JonBJ 2 Replies Last reply
                          0
                          • E El3ctroGh0st

                            @shaan7 Ahh yes, now it seems to reach the checkpoint. Thanks! I'm wondering though, what is the difference here between making it a pointer and now? Does making it a pointer prevent it from being deleted after going out of scope?

                            S Offline
                            S Offline
                            shaan7
                            wrote on last edited by
                            #13

                            @El3ctroGh0st said in QNetwork doesn't work:

                            Does making it a pointer prevent it from being deleted after going out of scope?

                            Yes. And passing this as a parent when using new makes sure that your WeatherReader will be deleted when WeatherGetter is deleted. See http://doc.qt.io/qt-5/objecttrees.html

                            1 Reply Last reply
                            4
                            • E El3ctroGh0st

                              @shaan7 Ahh yes, now it seems to reach the checkpoint. Thanks! I'm wondering though, what is the difference here between making it a pointer and now? Does making it a pointer prevent it from being deleted after going out of scope?

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #14

                              @El3ctroGh0st

                              making it a pointer prevent it from being deleted after going out of scope?

                              Sure! You doing a new means you are responsible for doing a delete at some point, else it leaks. Local variable like you have now disappears when the function it's in exits; or make it a class member variable to keep it alive throughout the instance life-time.

                              1 Reply Last reply
                              0
                              • E Offline
                                E Offline
                                El3ctroGh0st
                                wrote on last edited by
                                #15

                                Got it. Thanks to you two!

                                1 Reply Last reply
                                1

                                • Login

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