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. How to get information from QNetworkReply
Qt 6.11 is out! See what's new in the release blog

How to get information from QNetworkReply

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 1.4k Views 2 Watching
  • 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.
  • U Offline
    U Offline
    undac
    wrote on last edited by
    #1

    Hi,

    I am using QNetworkManager, and i want to make a get request to an url and print the reply information to a file, but i seem to have a problem with my reply as the information is not available and the ReadyRead or Finished signals are not being triggered.

    std::string url = ui.searchLine->text().toStdString();
    			QUrl myurl = QUrl(url.c_str());
    			QNetworkReply* reply = man->get(QNetworkRequest(myurl));
    			connect(reply, &QNetworkReply::readyRead, [&]()
    				{
    					QByteArray read = reply->readAll();
    					QFile out("page.html");
    					out.open(QIODevice::WriteOnly|QIODevice::Text);
    					out.write(read);
    					out.close();
    					reply->close();
    					reply->deleteLater();
    				});
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      Maybe try using QUrl
      manager->get(QNetworkRequest(QUrl(myurl)));

      https://www.bogotobogo.com/Qt/Qt5_Downloading_Files_QNetworkAccessManager_QNetworkRequest.php

      1 Reply Last reply
      0
      • U Offline
        U Offline
        undac
        wrote on last edited by
        #3

        It seems to have no effect, and i can't seem to be able to see the value of reply!(https://ibb.co/BtYTBy0)

        mrjjM 1 Reply Last reply
        0
        • U undac

          It seems to have no effect, and i can't seem to be able to see the value of reply!(https://ibb.co/BtYTBy0)

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @undac
          well now it enters the ReadReady it seems ?
          So check the errors as seen in link.
          maybe be some error if read variable is empty.

           if(reply->error())
              {
                  qDebug() << "ERROR!";
                  qDebug() << reply->errorString();
              }
          
          1 Reply Last reply
          0
          • U Offline
            U Offline
            undac
            wrote on last edited by
            #5

            When running it now though, it throws me an acces violation reading location exception at the reply->readAll()

            mrjjM 1 Reply Last reply
            0
            • U undac

              When running it now though, it throws me an acces violation reading location exception at the reply->readAll()

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              Hi
              Then there must be something with the capture of reply variable if its
              invalid inside the lambda.

              try
              [=]
              and not [&]
              as maybe it captures the local reply and its out of scope when lambda runs.

              1 Reply Last reply
              1
              • Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @mrjj said in How to get information from QNetworkReply:

                as maybe it captures the local reply and its out of scope when lambda runs.

                [&] captures by reference and the local pointer is no longer valid due to this.
                [=] is also not really useful - simply copying all is like using a hammer to open an egg... better is to only copy what you really need -> [this, reply]

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                4
                • U Offline
                  U Offline
                  undac
                  wrote on last edited by
                  #8

                  It does work both with the [=] syntax and the [this, reply] one, but i opted for the latter. I noticed that it didn't download the data properly and so i changed to &QNetworkReply::finished and it works as desired.

                  Thank you very much!

                  Christian EhrlicherC 1 Reply Last reply
                  1
                  • U undac

                    It does work both with the [=] syntax and the [this, reply] one, but i opted for the latter. I noticed that it didn't download the data properly and so i changed to &QNetworkReply::finished and it works as desired.

                    Thank you very much!

                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @undac Then please mark this topic as solved, thx.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    1

                    • Login

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