QNetwork doesn't work
-
@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 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? -
@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(); } -
@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
YourWeatherReader wReadergoes out of scope at end ofWeatherGetter(), presumably taking the slot with it? -
@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 @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
mwReaderas a private member variable of theWeatherGetterclass and use that. -
@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
mwReaderas a private member variable of theWeatherGetterclass 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?
-
@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
thisas a parent when usingnewmakes sure that yourWeatherReaderwill be deleted whenWeatherGetteris deleted. See http://doc.qt.io/qt-5/objecttrees.html -
@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?
making it a pointer prevent it from being deleted after going out of scope?
Sure! You doing a
newmeans you are responsible for doing adeleteat 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!