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
Forum Updated to NodeBB v4.3 + New Features

QNetwork doesn't work

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 3 Posters 2.2k 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.
  • 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