Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QNetwork doesn't work



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



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



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



  • @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 :)



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



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



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



  • @El3ctroGh0st can you paste your main.cpp?



  • @shaan7

    #include "weathergetter.hpp"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        WeatherGetter w;
        w.show();
    
        return a.exec();
    }
    


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



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



  • @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?



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



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



  • Got it. Thanks to you two!


Log in to reply