Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. qnetwork reply not emitted finished
Forum Updated to NodeBB v4.3 + New Features

qnetwork reply not emitted finished

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 880 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • faduF Offline
    faduF Offline
    fadu
    wrote on last edited by
    #1

    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();
    }
    
    JonBJ 1 Reply Last reply
    0
    • faduF fadu

      @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();
          });
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #8

      @fadu
      So your QNetworkAccessManager 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 reply finished signal? If you make your QNetworkAccessManager networkManager a class member variable, or new & delete the instance later, does your code then work?

      1 Reply Last reply
      1
      • faduF fadu

        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();
        }
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @fadu
        Does a.exec() return immediately "in GUI app" since you show no window?

        1 Reply Last reply
        1
        • faduF Offline
          faduF Offline
          fadu
          wrote on last edited by
          #3

          @JonB
          No i didn't use it in gui app
          this is ui code

           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();
          
              });
          
          JonBJ 1 Reply Last reply
          0
          • faduF fadu

            @JonB
            No i didn't use it in gui app
            this is ui code

             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();
            
                });
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @fadu
            Just checking that the lifetime of QNetworkAccessManager networkManager lasts until finished? Is it e.g. a member variable of some class?

            1 Reply Last reply
            0
            • faduF Offline
              faduF Offline
              fadu
              wrote on last edited by fadu
              #5

              @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

              JonBJ 1 Reply Last reply
              0
              • faduF fadu

                @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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #6

                @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 no a.exec(). Can you show your actual non-working UI program? Or confirm what the lifetime/scope of QNetworkAccessManager networkManager is?

                1 Reply Last reply
                0
                • faduF Offline
                  faduF Offline
                  fadu
                  wrote on last edited by fadu
                  #7

                  @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();
                      });
                  }
                  
                  JonBJ 1 Reply Last reply
                  0
                  • faduF fadu

                    @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();
                        });
                    }
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #8

                    @fadu
                    So your QNetworkAccessManager 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 reply finished signal? If you make your QNetworkAccessManager networkManager a class member variable, or new & delete the instance later, does your code then work?

                    1 Reply Last reply
                    1
                    • faduF Offline
                      faduF Offline
                      fadu
                      wrote on last edited by
                      #9

                      @JonB
                      that's it thank you very much

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved