Solved QNetwork doesn't work
-
@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!