qnetwork reply not emitted finished
-
good day for all
i have this code when i run it in console application it's 100% working but when but same code in gui app qnetwork reply didn't emit finished signal!#include <QCoreApplication> #include <QJsonObject> #include <QUrlQuery> #include <QtNetwork/QNetworkAccessManager> #include <QtNetwork/QNetworkReply> #include<QJsonDocument> #include <QHostAddress> #include <QHostInfo> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QNetworkAccessManager networkManager; QUrl url("https://api.ipify.org"); //the query used to add the parameter "format=json" to the request QUrlQuery query; query.addQueryItem("format", "json"); //set the query on the url url.setQuery(query); //make a *get* request using the above url QNetworkReply* reply = networkManager.get(QNetworkRequest(url)); QObject::connect(reply, &QNetworkReply::finished, [=](){ if(reply->error() != QNetworkReply::NoError) { //failure qDebug() << "error: " << reply->error(); } else { QJsonObject jsonObject= QJsonDocument::fromJson(reply->readAll()).object(); QHostAddress ip(jsonObject["ip"].toString()); qDebug() << "external ip: " << ip; } reply->deleteLater(); }); return a.exec(); }
-
@JonB
yes totally like you saw
this is gui application code#include "mainwindow.h" #include "ui_mainwindow.h" #include <QJsonObject> #include <QUrlQuery> #include <QtNetwork/QNetworkAccessManager> #include <QtNetwork/QNetworkReply> #include<QJsonDocument> #include <QHostAddress> #include <QHostInfo> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { QNetworkAccessManager networkManager; QUrl url("https://api.ipify.org"); //the query used to add the parameter "format=json" to the request QUrlQuery query; query.addQueryItem("format", "json"); //set the query on the url url.setQuery(query); //make a *get* request using the above url QNetworkReply* reply = networkManager.get(QNetworkRequest(url)); QObject::connect(reply, &QNetworkReply::finished, [=](){ if(reply->error() != QNetworkReply::NoError) { //failure qDebug() << "error: " << reply->error(); } else { QJsonObject jsonObject= QJsonDocument::fromJson(reply->readAll()).object(); QHostAddress ip(jsonObject["ip"].toString()); qDebug() << "external ip: " << ip; } reply->deleteLater(); }); }
@fadu
So yourQNetworkAccessManager networkManager;
is a local variable in the pushbutton slot. It is destroyed at the end of that method. I have never used QNAM but I would assume/guess its lifetime must last until after the replyfinished
signal? If you make yourQNetworkAccessManager networkManager
a class member variable, ornew
&delete
the instance later, does your code then work? -
good day for all
i have this code when i run it in console application it's 100% working but when but same code in gui app qnetwork reply didn't emit finished signal!#include <QCoreApplication> #include <QJsonObject> #include <QUrlQuery> #include <QtNetwork/QNetworkAccessManager> #include <QtNetwork/QNetworkReply> #include<QJsonDocument> #include <QHostAddress> #include <QHostInfo> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QNetworkAccessManager networkManager; QUrl url("https://api.ipify.org"); //the query used to add the parameter "format=json" to the request QUrlQuery query; query.addQueryItem("format", "json"); //set the query on the url url.setQuery(query); //make a *get* request using the above url QNetworkReply* reply = networkManager.get(QNetworkRequest(url)); QObject::connect(reply, &QNetworkReply::finished, [=](){ if(reply->error() != QNetworkReply::NoError) { //failure qDebug() << "error: " << reply->error(); } else { QJsonObject jsonObject= QJsonDocument::fromJson(reply->readAll()).object(); QHostAddress ip(jsonObject["ip"].toString()); qDebug() << "external ip: " << ip; } reply->deleteLater(); }); return a.exec(); }
-
@JonB
No i didn't use it in gui app
this is ui codeQNetworkAccessManager networkManager; QUrl url("https://api.ipify.org"); //the query used to add the parameter "format=json" to the request QUrlQuery query; query.addQueryItem("format", "json"); //set the query on the url url.setQuery(query); //make a *get* request using the above url QNetworkReply* reply = networkManager.get(QNetworkRequest(url)); QObject::connect(reply, &QNetworkReply::finished, [=](){ if(reply->error() != QNetworkReply::NoError) { //failure qDebug() << "error: " << reply->error(); } else { QJsonObject jsonObject= QJsonDocument::fromJson(reply->readAll()).object(); QHostAddress ip(jsonObject["ip"].toString()); qDebug() << "external ip: " << ip; } reply->deleteLater(); });
-
@JonB
No i didn't use it in gui app
this is ui codeQNetworkAccessManager networkManager; QUrl url("https://api.ipify.org"); //the query used to add the parameter "format=json" to the request QUrlQuery query; query.addQueryItem("format", "json"); //set the query on the url url.setQuery(query); //make a *get* request using the above url QNetworkReply* reply = networkManager.get(QNetworkRequest(url)); QObject::connect(reply, &QNetworkReply::finished, [=](){ if(reply->error() != QNetworkReply::NoError) { //failure qDebug() << "error: " << reply->error(); } else { QJsonObject jsonObject= QJsonDocument::fromJson(reply->readAll()).object(); QHostAddress ip(jsonObject["ip"].toString()); qDebug() << "external ip: " << ip; } reply->deleteLater(); });
-
@JonB
no i put it in new test app there is no classes or anything else just the code above
still same problem
and tried define recieve signal separately@fadu
If I understand you right: the first code you posted with the question is actually your working code, in a non GUI-app? You say it's not working in a UI app, then you say you the code is not the same there. It's not great to post code which works, we need to see code which does not.You second code, for a "GUI" app, does not show a complete program. E.g. it has no
main()
, it has noa.exec()
. Can you show your actual non-working UI program? Or confirm what the lifetime/scope ofQNetworkAccessManager networkManager
is? -
@JonB
yes totally like you saw
this is gui application code#include "mainwindow.h" #include "ui_mainwindow.h" #include <QJsonObject> #include <QUrlQuery> #include <QtNetwork/QNetworkAccessManager> #include <QtNetwork/QNetworkReply> #include<QJsonDocument> #include <QHostAddress> #include <QHostInfo> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { QNetworkAccessManager networkManager; QUrl url("https://api.ipify.org"); //the query used to add the parameter "format=json" to the request QUrlQuery query; query.addQueryItem("format", "json"); //set the query on the url url.setQuery(query); //make a *get* request using the above url QNetworkReply* reply = networkManager.get(QNetworkRequest(url)); QObject::connect(reply, &QNetworkReply::finished, [=](){ if(reply->error() != QNetworkReply::NoError) { //failure qDebug() << "error: " << reply->error(); } else { QJsonObject jsonObject= QJsonDocument::fromJson(reply->readAll()).object(); QHostAddress ip(jsonObject["ip"].toString()); qDebug() << "external ip: " << ip; } reply->deleteLater(); }); }
-
@JonB
yes totally like you saw
this is gui application code#include "mainwindow.h" #include "ui_mainwindow.h" #include <QJsonObject> #include <QUrlQuery> #include <QtNetwork/QNetworkAccessManager> #include <QtNetwork/QNetworkReply> #include<QJsonDocument> #include <QHostAddress> #include <QHostInfo> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { QNetworkAccessManager networkManager; QUrl url("https://api.ipify.org"); //the query used to add the parameter "format=json" to the request QUrlQuery query; query.addQueryItem("format", "json"); //set the query on the url url.setQuery(query); //make a *get* request using the above url QNetworkReply* reply = networkManager.get(QNetworkRequest(url)); QObject::connect(reply, &QNetworkReply::finished, [=](){ if(reply->error() != QNetworkReply::NoError) { //failure qDebug() << "error: " << reply->error(); } else { QJsonObject jsonObject= QJsonDocument::fromJson(reply->readAll()).object(); QHostAddress ip(jsonObject["ip"].toString()); qDebug() << "external ip: " << ip; } reply->deleteLater(); }); }
@fadu
So yourQNetworkAccessManager networkManager;
is a local variable in the pushbutton slot. It is destroyed at the end of that method. I have never used QNAM but I would assume/guess its lifetime must last until after the replyfinished
signal? If you make yourQNetworkAccessManager networkManager
a class member variable, ornew
&delete
the instance later, does your code then work?