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
Forum Updated to NodeBB v4.3 + New Features

Storing data from QnetworkReply

Scheduled Pinned Locked Moved Solved General and Desktop
45 Posts 8 Posters 14.7k Views 4 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.
  • 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
            • jsulmJ jsulm

              @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.

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

              @jsulm said in Storing data from QnetworkReply:

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

              Hi

              Do you mean another connection, something like this:

              void data::download(){
              
                 _qnam->get(QNetworkRequest(QUrl("http://www.google.com")));
                 QObject::connect(_qnam, SIGNAL(finished(QNetworkReply*)), this, SLOT(output(QNetworkReply*)));
                 QObject::connect(QIODevice, SIGNAL(readyRead()), this, SLOT(output(QNetworkReply*)));
              
              }
              

              I know the QIODevice usage here is wrong, but just trying to get an idea

              thanks

              jsulmJ 1 Reply Last reply
              0
              • L LeeH

                @jsulm said in Storing data from QnetworkReply:

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

                Hi

                Do you mean another connection, something like this:

                void data::download(){
                
                   _qnam->get(QNetworkRequest(QUrl("http://www.google.com")));
                   QObject::connect(_qnam, SIGNAL(finished(QNetworkReply*)), this, SLOT(output(QNetworkReply*)));
                   QObject::connect(QIODevice, SIGNAL(readyRead()), this, SLOT(output(QNetworkReply*)));
                
                }
                

                I know the QIODevice usage here is wrong, but just trying to get an idea

                thanks

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

                @LeeH No need for two connections, the one for readyRead() is enough

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

                L 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @LeeH No need for two connections, the one for readyRead() is enough

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

                  @jsulm

                  Ok... so how should I use QIOdevice here:

                  QObject::connect(QIODevice, SIGNAL(readyRead()), this, SLOT(output(QNetworkReply*)));
                  

                  i get error: "expecting primary expression before ',' token" and it can't be instantiated

                  thanks

                  m.sueM jsulmJ 2 Replies Last reply
                  0
                  • L LeeH

                    @jsulm

                    Ok... so how should I use QIOdevice here:

                    QObject::connect(QIODevice, SIGNAL(readyRead()), this, SLOT(output(QNetworkReply*)));
                    

                    i get error: "expecting primary expression before ',' token" and it can't be instantiated

                    thanks

                    m.sueM Offline
                    m.sueM Offline
                    m.sue
                    wrote on last edited by
                    #24

                    Hi @LeeH

                    You need the QIODevice type object not the class name there.

                    -Michael.

                    L 1 Reply Last reply
                    0
                    • m.sueM m.sue

                      Hi @LeeH

                      You need the QIODevice type object not the class name there.

                      -Michael.

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

                      @m.sue

                      Hi,

                      QIODevice is an abstract class.

                      m.sueM 1 Reply Last reply
                      0
                      • L LeeH

                        @m.sue

                        Hi,

                        QIODevice is an abstract class.

                        m.sueM Offline
                        m.sueM Offline
                        m.sue
                        wrote on last edited by
                        #26

                        Hi @LeeH

                        then the object of the derived class that you use.

                        -Michael.

                        L 1 Reply Last reply
                        0
                        • m.sueM m.sue

                          Hi @LeeH

                          then the object of the derived class that you use.

                          -Michael.

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

                          @m.sue said in Storing data from QnetworkReply:

                          then the object of the derived class that you use.

                          I modified my code to use readRead() from QNetworkReply object and it still doesn't work:

                          ...
                          void data::download(){
                          
                          _rep = _qnam->get(QNetworkRequest(QUrl("http://www.google.com")));
                          QObject::connect(_rep, SIGNAL(readyRead()), this, SLOT(output()));
                          
                          }
                          
                          void data::output(){
                          
                              if(!_rep->error()){
                          
                                 _ba.append(_rep->readAll());
                          
                              }
                              else{
                                  qDebug() << _rep->error();
                              }
                              _rep->deleteLater();
                          }
                          
                          m.sueM 1 Reply Last reply
                          0
                          • L LeeH

                            @m.sue said in Storing data from QnetworkReply:

                            then the object of the derived class that you use.

                            I modified my code to use readRead() from QNetworkReply object and it still doesn't work:

                            ...
                            void data::download(){
                            
                            _rep = _qnam->get(QNetworkRequest(QUrl("http://www.google.com")));
                            QObject::connect(_rep, SIGNAL(readyRead()), this, SLOT(output()));
                            
                            }
                            
                            void data::output(){
                            
                                if(!_rep->error()){
                            
                                   _ba.append(_rep->readAll());
                            
                                }
                                else{
                                    qDebug() << _rep->error();
                                }
                                _rep->deleteLater();
                            }
                            
                            m.sueM Offline
                            m.sueM Offline
                            m.sue
                            wrote on last edited by
                            #28

                            Hi @LeeH

                            I would write: connect(_rep, SIGNAL(readyRead()), SLOT(output())); But this should not be essential.

                            So maybe output() is not a slot.

                            -Michael.

                            L 1 Reply Last reply
                            0
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #29

                              @LeeH said in Storing data from QnetworkReply:

                              qDebug() << _rep->error();

                              Is line ever activated ?
                              is _ba.append called ?

                              L 1 Reply Last reply
                              0
                              • m.sueM m.sue

                                Hi @LeeH

                                I would write: connect(_rep, SIGNAL(readyRead()), SLOT(output())); But this should not be essential.

                                So maybe output() is not a slot.

                                -Michael.

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

                                @m.sue said in Storing data from QnetworkReply:

                                So maybe output() is not a slot.

                                Hi it's always been a public slot... and i do get server reply data

                                1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  @LeeH said in Storing data from QnetworkReply:

                                  qDebug() << _rep->error();

                                  Is line ever activated ?
                                  is _ba.append called ?

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

                                  @mrjj said in Storing data from QnetworkReply:

                                  Is line ever activated ?
                                  is _ba.append called ?

                                  Hi, there are no errors from server, and I always get reply.
                                  I call _ba from main() using data::get()

                                  mrjjM 1 Reply Last reply
                                  0
                                  • L LeeH

                                    @mrjj said in Storing data from QnetworkReply:

                                    Is line ever activated ?
                                    is _ba.append called ?

                                    Hi, there are no errors from server, and I always get reply.
                                    I call _ba from main() using data::get()

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

                                    @LeeH
                                    but is data appended?

                                    if(!_rep->error()){
                                    _ba.append(_rep->readAll());
                                    qDebug() <<"size" << _ba.size();
                                    }

                                    have you tried with a breakpoint ?
                                    is data::output() called?

                                    L 1 Reply Last reply
                                    0
                                    • mrjjM mrjj

                                      @LeeH
                                      but is data appended?

                                      if(!_rep->error()){
                                      _ba.append(_rep->readAll());
                                      qDebug() <<"size" << _ba.size();
                                      }

                                      have you tried with a breakpoint ?
                                      is data::output() called?

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

                                      @mrjj

                                      When I added:

                                      qDebug() <<"size" << _ba.size();

                                      i got:

                                      size 2634
                                      

                                      Yes data::output() is definately being called

                                      mrjjM 1 Reply Last reply
                                      0
                                      • L LeeH

                                        @mrjj

                                        When I added:

                                        qDebug() <<"size" << _ba.size();

                                        i got:

                                        size 2634
                                        

                                        Yes data::output() is definately being called

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

                                        @LeeH
                                        So what is not working ?
                                        You clearly read data into _ba.

                                        L 1 Reply Last reply
                                        0
                                        • mrjjM mrjj

                                          @LeeH
                                          So what is not working ?
                                          You clearly read data into _ba.

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

                                          @mrjj

                                          but my data::get() I use to read _ba that i call from main() after downloading, is empty(""), where as if I read _ba from the slot inside the class I can see all my data

                                          L 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