Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Read Data From URL
Qt 6.12 LTS is out! See what's new in the release blog

Read Data From URL

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 3 Posters 4.3k 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.
  • Ketan__Patel__0011K Offline
    Ketan__Patel__0011K Offline
    Ketan__Patel__0011
    wrote on last edited by Ketan__Patel__0011
    #1

    Hello Friends And Qt Experts

    i am trying to read data from any particular URL

    But between the process my application is hang or not responding

            qDebug() << "Pointer --> 1";
            QNetworkAccessManager  manager;
            qDebug() << "Pointer --> 2";
            QNetworkReply *response = manager.get(QNetworkRequest(QUrl(QString::fromStdString("MY_URL"))));
            qDebug() << "Pointer --> 3";
            QEventLoop event;
            qDebug() << "Pointer --> 4";
            QObject::connect(response, SIGNAL(finished()), &event, SLOT(quit()));
            qDebug() << "Pointer --> 5";
            event.exec(); 
            qDebug() << "Pointer --> 6";
            QString content = response->readAll();
            qDebug() << "Pointer --> 7";
            std::string readBuffer = content.toStdString();
            qDebug() << "Pointer --> 8";
    

    When my pointer is reach At event.exec() that time my application is hang or not responding

    i am not getting Next message after "Pointer --> 5"

    please help to solve this problem

    Thanks in Advance

    JonBJ jsulmJ 2 Replies Last reply
    0
    • Ketan__Patel__0011K Ketan__Patel__0011

      Hello Friends And Qt Experts

      i am trying to read data from any particular URL

      But between the process my application is hang or not responding

              qDebug() << "Pointer --> 1";
              QNetworkAccessManager  manager;
              qDebug() << "Pointer --> 2";
              QNetworkReply *response = manager.get(QNetworkRequest(QUrl(QString::fromStdString("MY_URL"))));
              qDebug() << "Pointer --> 3";
              QEventLoop event;
              qDebug() << "Pointer --> 4";
              QObject::connect(response, SIGNAL(finished()), &event, SLOT(quit()));
              qDebug() << "Pointer --> 5";
              event.exec(); 
              qDebug() << "Pointer --> 6";
              QString content = response->readAll();
              qDebug() << "Pointer --> 7";
              std::string readBuffer = content.toStdString();
              qDebug() << "Pointer --> 8";
      

      When my pointer is reach At event.exec() that time my application is hang or not responding

      i am not getting Next message after "Pointer --> 5"

      please help to solve this problem

      Thanks in Advance

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

      @Ketan__Patel__0011
      Why are you using any event.exec()? You are supposed to read from the response in the finished() slot, with no quitting. (Your response/reply/manager object should be scoped so it/they live until then.)

      Ketan__Patel__0011K 1 Reply Last reply
      2
      • Ketan__Patel__0011K Ketan__Patel__0011

        Hello Friends And Qt Experts

        i am trying to read data from any particular URL

        But between the process my application is hang or not responding

                qDebug() << "Pointer --> 1";
                QNetworkAccessManager  manager;
                qDebug() << "Pointer --> 2";
                QNetworkReply *response = manager.get(QNetworkRequest(QUrl(QString::fromStdString("MY_URL"))));
                qDebug() << "Pointer --> 3";
                QEventLoop event;
                qDebug() << "Pointer --> 4";
                QObject::connect(response, SIGNAL(finished()), &event, SLOT(quit()));
                qDebug() << "Pointer --> 5";
                event.exec(); 
                qDebug() << "Pointer --> 6";
                QString content = response->readAll();
                qDebug() << "Pointer --> 7";
                std::string readBuffer = content.toStdString();
                qDebug() << "Pointer --> 8";
        

        When my pointer is reach At event.exec() that time my application is hang or not responding

        i am not getting Next message after "Pointer --> 5"

        please help to solve this problem

        Thanks in Advance

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Ketan__Patel__0011 said in Read Data From URL:

        When my pointer is reach At event.exec() that time my application is hang or not responding

        Of course it does. exec() is a blocking call. Why do you use a local event loop?
        Qt is an asynchronous framework, you should use it as such.
        There are also networking examples showing how to do it properly.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • JonBJ JonB

          @Ketan__Patel__0011
          Why are you using any event.exec()? You are supposed to read from the response in the finished() slot, with no quitting. (Your response/reply/manager object should be scoped so it/they live until then.)

          Ketan__Patel__0011K Offline
          Ketan__Patel__0011K Offline
          Ketan__Patel__0011
          wrote on last edited by
          #4

          @JonB

          Thanks For The Reply

          But when i remove the event.exec() and run the application then i didn't the response.

          jsulmJ JonBJ 2 Replies Last reply
          0
          • Ketan__Patel__0011K Ketan__Patel__0011

            @JonB

            Thanks For The Reply

            But when i remove the event.exec() and run the application then i didn't the response.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Ketan__Patel__0011 said in Read Data From URL:

            and run the application then i didn't the response.

            Because your QNetworkAccessManager manager; is a local variable...

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            Ketan__Patel__0011K 1 Reply Last reply
            1
            • Ketan__Patel__0011K Ketan__Patel__0011

              @JonB

              Thanks For The Reply

              But when i remove the event.exec() and run the application then i didn't the response.

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

              @Ketan__Patel__0011
              That's why I wrote

              Your response/reply/manager object should be scoped so it/they live until then.

              So as @jsulm is saying, you have to look at the scope/lifetime of the variables you are using.

              1 Reply Last reply
              0
              • jsulmJ jsulm

                @Ketan__Patel__0011 said in Read Data From URL:

                and run the application then i didn't the response.

                Because your QNetworkAccessManager manager; is a local variable...

                Ketan__Patel__0011K Offline
                Ketan__Patel__0011K Offline
                Ketan__Patel__0011
                wrote on last edited by
                #7

                @jsulm

                So i Have to make this variable QNetworkAccessManager manager; as a class member.

                jsulmJ 1 Reply Last reply
                1
                • Ketan__Patel__0011K Ketan__Patel__0011

                  @jsulm

                  So i Have to make this variable QNetworkAccessManager manager; as a class member.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Ketan__Patel__0011 Yes

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Ketan__Patel__0011K 1 Reply Last reply
                  1
                  • jsulmJ jsulm

                    @Ketan__Patel__0011 Yes

                    Ketan__Patel__0011K Offline
                    Ketan__Patel__0011K Offline
                    Ketan__Patel__0011
                    wrote on last edited by Ketan__Patel__0011
                    #9

                    @jsulm
                    I am remove this line event.exec(); from my code And make QNetworkAccessManager manager; variable as a class member
                    but still i didn't the the response.

                    jsulmJ 1 Reply Last reply
                    0
                    • Ketan__Patel__0011K Ketan__Patel__0011

                      @jsulm
                      I am remove this line event.exec(); from my code And make QNetworkAccessManager manager; variable as a class member
                      but still i didn't the the response.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by jsulm
                      #10

                      @Ketan__Patel__0011 Do you mean finished signal was not emitted?
                      You should add error handling to your code.
                      And you CAN'T read the response just after sending the request, do it in a slot connected to finished signal...

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      Ketan__Patel__0011K 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @Ketan__Patel__0011 Do you mean finished signal was not emitted?
                        You should add error handling to your code.
                        And you CAN'T read the response just after sending the request, do it in a slot connected to finished signal...

                        Ketan__Patel__0011K Offline
                        Ketan__Patel__0011K Offline
                        Ketan__Patel__0011
                        wrote on last edited by
                        #11

                        @jsulm

                        response->errorString() function give me this error message
                        Error Is : Unknown error

                        jsulmJ 1 Reply Last reply
                        0
                        • Ketan__Patel__0011K Ketan__Patel__0011

                          @jsulm

                          response->errorString() function give me this error message
                          Error Is : Unknown error

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @Ketan__Patel__0011 As I said: you can't read response just after sending request. This is not how asynchronous APIs work. Read response in a slot connected ti finished signal. This is also what @JonB already wrote.

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          Ketan__Patel__0011K 1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @Ketan__Patel__0011 As I said: you can't read response just after sending request. This is not how asynchronous APIs work. Read response in a slot connected ti finished signal. This is also what @JonB already wrote.

                            Ketan__Patel__0011K Offline
                            Ketan__Patel__0011K Offline
                            Ketan__Patel__0011
                            wrote on last edited by
                            #13

                            @jsulm

                            Thanks For Reply

                            So Do you have any Suggestion or Any Sample Code Or Any Block Of Code For it

                            jsulmJ 1 Reply Last reply
                            0
                            • Ketan__Patel__0011K Ketan__Patel__0011

                              @jsulm

                              Thanks For Reply

                              So Do you have any Suggestion or Any Sample Code Or Any Block Of Code For it

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @Ketan__Patel__0011 Did you read the documentation? https://doc.qt.io/qt-5/qnetworkaccessmanager.html

                              QNetworkAccessManager *manager = new QNetworkAccessManager(this);
                              connect(manager, &QNetworkAccessManager::finished,
                                      this, &MyClass::replyFinished);
                              
                              manager->get(QNetworkRequest(QUrl("http://qt-project.org")));
                              

                              In MyClass::replyFinished you read the response.

                              And there are many examples: https://doc.qt.io/qt-5/examples-network.html

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              Ketan__Patel__0011K 1 Reply Last reply
                              0
                              • jsulmJ jsulm

                                @Ketan__Patel__0011 Did you read the documentation? https://doc.qt.io/qt-5/qnetworkaccessmanager.html

                                QNetworkAccessManager *manager = new QNetworkAccessManager(this);
                                connect(manager, &QNetworkAccessManager::finished,
                                        this, &MyClass::replyFinished);
                                
                                manager->get(QNetworkRequest(QUrl("http://qt-project.org")));
                                

                                In MyClass::replyFinished you read the response.

                                And there are many examples: https://doc.qt.io/qt-5/examples-network.html

                                Ketan__Patel__0011K Offline
                                Ketan__Patel__0011K Offline
                                Ketan__Patel__0011
                                wrote on last edited by
                                #15

                                @jsulm

                                Thanks for the reply

                                I am try all are the solution

                                And Create One Small QWidget Application Example For it and my problem is solved but..
                                i am create one library in Qt any put my same code in my Class library functions
                                that time i didn't get the response from my URL.

                                jsulmJ 1 Reply Last reply
                                0
                                • Ketan__Patel__0011K Ketan__Patel__0011

                                  @jsulm

                                  Thanks for the reply

                                  I am try all are the solution

                                  And Create One Small QWidget Application Example For it and my problem is solved but..
                                  i am create one library in Qt any put my same code in my Class library functions
                                  that time i didn't get the response from my URL.

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @Ketan__Patel__0011 In what app do you use that library? Does that app have a running Qt event loop?

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  Ketan__Patel__0011K 1 Reply Last reply
                                  0
                                  • jsulmJ jsulm

                                    @Ketan__Patel__0011 In what app do you use that library? Does that app have a running Qt event loop?

                                    Ketan__Patel__0011K Offline
                                    Ketan__Patel__0011K Offline
                                    Ketan__Patel__0011
                                    wrote on last edited by
                                    #17

                                    @jsulm

                                    Actually i am create My Library in QT And i am export The Some Function From My Library and used in others application (DLLImport And DLLExport)

                                    jsulmJ 1 Reply Last reply
                                    0
                                    • Ketan__Patel__0011K Ketan__Patel__0011

                                      @jsulm

                                      Actually i am create My Library in QT And i am export The Some Function From My Library and used in others application (DLLImport And DLLExport)

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      @Ketan__Patel__0011 But does this application have a Qt event loop?

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      Ketan__Patel__0011K 1 Reply Last reply
                                      0
                                      • jsulmJ jsulm

                                        @Ketan__Patel__0011 But does this application have a Qt event loop?

                                        Ketan__Patel__0011K Offline
                                        Ketan__Patel__0011K Offline
                                        Ketan__Patel__0011
                                        wrote on last edited by
                                        #19

                                        @jsulm
                                        No that Application is not Qt Based But My Library Is Qt Based

                                        jsulmJ 1 Reply Last reply
                                        0
                                        • Ketan__Patel__0011K Ketan__Patel__0011

                                          @jsulm
                                          No that Application is not Qt Based But My Library Is Qt Based

                                          jsulmJ Offline
                                          jsulmJ Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          @Ketan__Patel__0011 said in Read Data From URL:

                                          But My Library Is Qt Based

                                          That is clear. But without Qt event loop Qt networking isn't going to work. You could execute networking part (from your DLL) in a thread with Qt event loop.

                                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          1 Reply Last reply
                                          3

                                          • Login

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