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. Storing data from QnetworkReply
QtWS25 Last Chance

Storing data from QnetworkReply

Scheduled Pinned Locked Moved Solved General and Desktop
45 Posts 8 Posters 13.8k 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.
  • L Offline
    L Offline
    LeeH
    wrote on last edited by
    #1

    Hi everyone

    Just a quick question, is there any way of storing data from QNetworkReply other than downloading to file? It seems to be the only way I can access that data normally, like outside of the class. Thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You can use a QByteArray as a buffer to keep the data in memory. Just append new data to it as they arriving.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply
      3
      • SGaistS SGaist

        Hi,

        You can use a QByteArray as a buffer to keep the data in memory. Just append new data to it as they arriving.

        L Offline
        L Offline
        LeeH
        wrote on last edited by LeeH
        #3

        @SGaist

        Hi,
        small example I tried, something like this?

         void output(QNetworkReply* rep){
        
            if(!rep->error()){
                ba.append(rep->readAll());
            }
            rep->deleteLater();
        
        }
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Yes, except it looks like you are doing this from a free standing function. Is that the case ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          L 1 Reply Last reply
          0
          • SGaistS SGaist

            Yes, except it looks like you are doing this from a free standing function. Is that the case ?

            L Offline
            L Offline
            LeeH
            wrote on last edited by
            #5

            @SGaist Hi,

            Its a small example class (just header) so ba is an object, but it still dosen't work. Don't know what I'm doing wrong...

            A 1 Reply Last reply
            0
            • L LeeH

              @SGaist Hi,

              Its a small example class (just header) so ba is an object, but it still dosen't work. Don't know what I'm doing wrong...

              A Offline
              A Offline
              ambershark
              wrote on last edited by
              #6

              @LeeH What isn't working? What do you expect to happen? How are you trying to get/use the data later?

              My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

              L 1 Reply Last reply
              0
              • A ambershark

                @LeeH What isn't working? What do you expect to happen? How are you trying to get/use the data later?

                L Offline
                L Offline
                LeeH
                wrote on last edited by
                #7

                @ambershark Hi,

                I'm trying to treat 'ba' in my example like any other class data member, so if im in main() I call a get method for 'ba' but it's always empty because the qnetworkreply data expires after the slot. Saving reply data to file has always worked in the past but just seems overkill

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  LeeH
                  wrote on last edited by LeeH
                  #8

                  I found this https://wiki.qt.io/Download_Data_from_URL but it's still not storing data to class, only keeping it alive by emiting other signal:

                  void FileDownloader::fileDownloaded(QNetworkReply* pReply) {
                  m_DownloadedData = pReply->readAll();
                  //emit a signal
                  pReply->deleteLater();
                  emit downloaded();
                  }
                  
                  mrjjM raven-worxR 2 Replies Last reply
                  0
                  • L LeeH

                    I found this https://wiki.qt.io/Download_Data_from_URL but it's still not storing data to class, only keeping it alive by emiting other signal:

                    void FileDownloader::fileDownloaded(QNetworkReply* pReply) {
                    m_DownloadedData = pReply->readAll();
                    //emit a signal
                    pReply->deleteLater();
                    emit downloaded();
                    }
                    
                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #9

                    @LeeH
                    Something is a bit unclear

                    If you do

                    void output(QNetworkReply* rep){

                    if(!rep->error()){       
                        ba.append(rep->readAll());
                       qDebug() << " ba size=" << ba.size();
                    }
                    rep->deleteLater();
                    

                    }

                    Will it then grow at all ?

                    L 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @LeeH
                      Something is a bit unclear

                      If you do

                      void output(QNetworkReply* rep){

                      if(!rep->error()){       
                          ba.append(rep->readAll());
                         qDebug() << " ba size=" << ba.size();
                      }
                      rep->deleteLater();
                      

                      }

                      Will it then grow at all ?

                      L Offline
                      L Offline
                      LeeH
                      wrote on last edited by
                      #10

                      @mrjj

                      Hello

                      @SGaist said in Storing data from QnetworkReply:

                      You can use a QByteArray as a buffer to keep the data in memory. Just append new data to it as they arriving.

                      It was a suggestion that I was trying to make sense of in my example, so Im not sure tbh

                      1 Reply Last reply
                      0
                      • L LeeH

                        I found this https://wiki.qt.io/Download_Data_from_URL but it's still not storing data to class, only keeping it alive by emiting other signal:

                        void FileDownloader::fileDownloaded(QNetworkReply* pReply) {
                        m_DownloadedData = pReply->readAll();
                        //emit a signal
                        pReply->deleteLater();
                        emit downloaded();
                        }
                        
                        raven-worxR Offline
                        raven-worxR Offline
                        raven-worx
                        Moderators
                        wrote on last edited by
                        #11

                        @LeeH
                        are you calling your function upon the reply's finished signal?!

                        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                        If you have a question please use the forum so others can benefit from the solution in the future

                        L 1 Reply Last reply
                        0
                        • raven-worxR raven-worx

                          @LeeH
                          are you calling your function upon the reply's finished signal?!

                          L Offline
                          L Offline
                          LeeH
                          wrote on last edited by
                          #12

                          @raven-worx

                          No because If I do that, I will be able to read the data from the other function in that ba object but it will not be stored, so if i try to call that same function outside of the class it will be empty.

                          raven-worxR 1 Reply Last reply
                          0
                          • L LeeH

                            @raven-worx

                            No because If I do that, I will be able to read the data from the other function in that ba object but it will not be stored, so if i try to call that same function outside of the class it will be empty.

                            raven-worxR Offline
                            raven-worxR Offline
                            raven-worx
                            Moderators
                            wrote on last edited by
                            #13

                            @LeeH
                            but thats the point.
                            readAll() only returns the data when it tells you that it has finished.
                            Encapsulate your code into a helper class (each class has it's own QByteArray buffer).

                            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                            If you have a question please use the forum so others can benefit from the solution in the future

                            L 1 Reply Last reply
                            1
                            • raven-worxR raven-worx

                              @LeeH
                              but thats the point.
                              readAll() only returns the data when it tells you that it has finished.
                              Encapsulate your code into a helper class (each class has it's own QByteArray buffer).

                              L Offline
                              L Offline
                              LeeH
                              wrote on last edited by LeeH
                              #14

                              @raven-worx said in Storing data from QnetworkReply:

                              Encapsulate your code into a helper class (each class has it's own QByteArray buffer).

                              So would I have to set objects from other places directly to the reply data?

                              mrjjM 1 Reply Last reply
                              0
                              • L LeeH

                                @raven-worx said in Storing data from QnetworkReply:

                                Encapsulate your code into a helper class (each class has it's own QByteArray buffer).

                                So would I have to set objects from other places directly to the reply data?

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

                                @LeeH
                                Have a look at this sample
                                http://www.bogotobogo.com/Qt/Qt5_Downloading_Files_QNetworkAccessManager_QNetworkRequest.php
                                It uses a small helper class
                                class Downloader : public QObject
                                to manage the data it downloads. It responds to the finished signal.
                                Here it stores to file but you can as well store in ByteArray or whatever is needed.

                                Also its important to understand that you should NOT call the ReadData function yourself.
                                Your function will be called when data is ready. Its an asynchronous API.

                                L 2 Replies Last reply
                                0
                                • mrjjM mrjj

                                  @LeeH
                                  Have a look at this sample
                                  http://www.bogotobogo.com/Qt/Qt5_Downloading_Files_QNetworkAccessManager_QNetworkRequest.php
                                  It uses a small helper class
                                  class Downloader : public QObject
                                  to manage the data it downloads. It responds to the finished signal.
                                  Here it stores to file but you can as well store in ByteArray or whatever is needed.

                                  Also its important to understand that you should NOT call the ReadData function yourself.
                                  Your function will be called when data is ready. Its an asynchronous API.

                                  L Offline
                                  L Offline
                                  LeeH
                                  wrote on last edited by
                                  #16

                                  @mrjj

                                  Ok thanks for that, I will check it out.

                                  1 Reply Last reply
                                  0
                                  • mrjjM mrjj

                                    @LeeH
                                    Have a look at this sample
                                    http://www.bogotobogo.com/Qt/Qt5_Downloading_Files_QNetworkAccessManager_QNetworkRequest.php
                                    It uses a small helper class
                                    class Downloader : public QObject
                                    to manage the data it downloads. It responds to the finished signal.
                                    Here it stores to file but you can as well store in ByteArray or whatever is needed.

                                    Also its important to understand that you should NOT call the ReadData function yourself.
                                    Your function will be called when data is ready. Its an asynchronous API.

                                    L Offline
                                    L Offline
                                    LeeH
                                    wrote on last edited by LeeH
                                    #17

                                    @mrjj

                                    Have a look at this sample
                                    http://www.bogotobogo.com/Qt/Qt5_Downloading_Files_QNetworkAccessManager_QNetworkRequest.php
                                    It uses a small helper class
                                    class Downloader : public QObject
                                    to manage the data it downloads. It responds to the finished signal.
                                    Here it stores to file but you can as well store in ByteArray or whatever is needed.

                                    I already have a class doing this job - as I said in the begining I can already save my reply data to file that can be read/used from anywhere in one signal slot connection, I just want to do the same thing without using a file so it can be read/used from anywhere in a single connection. If my class data object that has been assigned (or appended to) the value of reply's byte array is always empty when called, say from outside of the class (after download), meaning I have to re-download everytime I need the same data, then the reply data is not stored to my object is it?

                                    mrjjM 1 Reply Last reply
                                    0
                                    • L LeeH

                                      @mrjj

                                      Have a look at this sample
                                      http://www.bogotobogo.com/Qt/Qt5_Downloading_Files_QNetworkAccessManager_QNetworkRequest.php
                                      It uses a small helper class
                                      class Downloader : public QObject
                                      to manage the data it downloads. It responds to the finished signal.
                                      Here it stores to file but you can as well store in ByteArray or whatever is needed.

                                      I already have a class doing this job - as I said in the begining I can already save my reply data to file that can be read/used from anywhere in one signal slot connection, I just want to do the same thing without using a file so it can be read/used from anywhere in a single connection. If my class data object that has been assigned (or appended to) the value of reply's byte array is always empty when called, say from outside of the class (after download), meaning I have to re-download everytime I need the same data, then the reply data is not stored to my object is it?

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

                                      @LeeH said in Storing data from QnetworkReply:
                                      Hi
                                      If you keep the instance of class Downloader around,
                                      there is no reason you cannot keep the data for later.
                                      Say if you append to a QByteArray variable defined as
                                      class member in Downloader

                                      Can you please show the actual code how you use your helper class and how you later
                                      try to access the data?

                                      L 1 Reply Last reply
                                      0
                                      • mrjjM mrjj

                                        @LeeH said in Storing data from QnetworkReply:
                                        Hi
                                        If you keep the instance of class Downloader around,
                                        there is no reason you cannot keep the data for later.
                                        Say if you append to a QByteArray variable defined as
                                        class member in Downloader

                                        Can you please show the actual code how you use your helper class and how you later
                                        try to access the data?

                                        L Offline
                                        L Offline
                                        LeeH
                                        wrote on last edited by
                                        #19

                                        @mrjj said in Storing data from QnetworkReply:

                                        Can you please show the actual code how you use your helper class and how you later
                                        try to access the data?

                                        Certainly.

                                        header:

                                        class data : public QObject{
                                        
                                            Q_OBJECT
                                        
                                        public:
                                            data();
                                            void download();
                                            QByteArray get();
                                        
                                        public slots:
                                        
                                            void output(QNetworkReply* rep);
                                        
                                        private:
                                            QNetworkAccessManager* _qnam;
                                            QByteArray _ba;
                                        
                                        };
                                        

                                        source:

                                        data::data(){}
                                        
                                        void data::download(){
                                        
                                            _qnam = new QNetworkAccessManager(this);
                                            _qnam->get(QNetworkRequest(QUrl("http://www.google.com")));
                                            QObject::connect(_qnam, SIGNAL(finished(QNetworkReply*)), this, SLOT(output(QNetworkReply*)));
                                        }
                                        
                                        void data::output(QNetworkReply* rep){
                                        
                                                if(!rep->error()){
                                        
                                                   _ba.append(rep->readAll());
                                        
                                                }
                                                rep->deleteLater();
                                            }
                                        
                                        QByteArray data::get(){
                                            qDebug() << _ba;
                                        }
                                        

                                        main:

                                         data obj;
                                         obj.download();
                                         obj.get();
                                        
                                        jsulmJ 1 Reply Last reply
                                        0
                                        • L LeeH

                                          @mrjj said in Storing data from QnetworkReply:

                                          Can you please show the actual code how you use your helper class and how you later
                                          try to access the data?

                                          Certainly.

                                          header:

                                          class data : public QObject{
                                          
                                              Q_OBJECT
                                          
                                          public:
                                              data();
                                              void download();
                                              QByteArray get();
                                          
                                          public slots:
                                          
                                              void output(QNetworkReply* rep);
                                          
                                          private:
                                              QNetworkAccessManager* _qnam;
                                              QByteArray _ba;
                                          
                                          };
                                          

                                          source:

                                          data::data(){}
                                          
                                          void data::download(){
                                          
                                              _qnam = new QNetworkAccessManager(this);
                                              _qnam->get(QNetworkRequest(QUrl("http://www.google.com")));
                                              QObject::connect(_qnam, SIGNAL(finished(QNetworkReply*)), this, SLOT(output(QNetworkReply*)));
                                          }
                                          
                                          void data::output(QNetworkReply* rep){
                                          
                                                  if(!rep->error()){
                                          
                                                     _ba.append(rep->readAll());
                                          
                                                  }
                                                  rep->deleteLater();
                                              }
                                          
                                          QByteArray data::get(){
                                              qDebug() << _ba;
                                          }
                                          

                                          main:

                                           data obj;
                                           obj.download();
                                           obj.get();
                                          
                                          jsulmJ Offline
                                          jsulmJ Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          @LeeH said in Storing data from QnetworkReply:

                                          QNetworkReply

                                          You should connect your output() slot to http://doc.qt.io/qt-5/qiodevice.html#readyRead signal.

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

                                          L 1 Reply Last reply
                                          2

                                          • Login

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