Solved 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/exitinggetWeatherInfo()
? Theget()
is asynchronous. You need to run the main event loop to get the signal, likeQApplication::exec()
or equivalent?
You ought also hook uperror
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 functionparseWeatherHTML
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 callinggetWeatherInfo()
, 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
? -
#include "weathergetter.hpp" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); WeatherGetter w; w.show(); return a.exec(); }
-
@El3ctroGh0st
YourWeatherReader wReader
goes out of scope at end ofWeatherGetter()
, 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 theWeatherGetter
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 usingnew
makes sure that yourWeatherReader
will be deleted whenWeatherGetter
is deleted. See http://doc.qt.io/qt-5/objecttrees.html -
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 adelete
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!