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 15.2k 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.
  • 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
                        • L LeeH

                          @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 Offline
                          L Offline
                          LeeH
                          wrote on last edited by
                          #36

                          @LeeH

                          You know I think It's best I just stick to saving to file, that seem's to be the only way I can be sure my data sticks around and can be used anywhere.... I thank everyone for their help, and apologize for wasting all of our time!

                          A 1 Reply Last reply
                          0
                          • L LeeH

                            @LeeH

                            You know I think It's best I just stick to saving to file, that seem's to be the only way I can be sure my data sticks around and can be used anywhere.... I thank everyone for their help, and apologize for wasting all of our time!

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

                            @LeeH Well here's your problem:

                            QByteArray data::get(){
                                qDebug() << _ba;
                            }
                            

                            You don't return anything. You should have gotten at least a compiler warning from that.

                            Anyway, just edit to be:

                            QByteArray data::get(){
                                qDebug() << _ba;
                                return _ba;
                            }
                            

                            And your data.get() will work.

                            There is no reason to save the data to a file. You're just wasting resources on disk and slowing your app down for no reason.

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

                            L 1 Reply Last reply
                            2
                            • 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

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

                              @LeeH said in Storing data from QnetworkReply:

                              Ok... so how should I use QIOdevice here

                              You use your QNetworkReply which is derived from QIODevice. And you cannot connect a class - you only can connect instances.
                              Did you read the documentation? http://doc.qt.io/qt-5/qnetworkaccessmanager.html
                              From there:

                              QNetworkRequest request;
                              request.setUrl(QUrl("http://qt-project.org"));
                              request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
                              
                              QNetworkReply *reply = manager->get(request);
                              connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead())); // THIS ONE
                              connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
                                      this, SLOT(slotError(QNetworkReply::NetworkError)));
                              connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
                                      this, SLOT(slotSslErrors(QList<QSslError>)));
                              

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

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

                                @LeeH said in Storing data from QnetworkReply:

                                data obj;
                                obj.download();
                                obj.get();

                                Ahh, now i understand the confusion.
                                It won't stay in obj.download() until download is finished,
                                but return at once, as QNetwork classes are asynchronous

                                Then you call obj.get() but no data is there, because it was not downloaded yet.

                                So it do work, but not in the way you think it does :)

                                1 Reply Last reply
                                3
                                • A ambershark

                                  @LeeH Well here's your problem:

                                  QByteArray data::get(){
                                      qDebug() << _ba;
                                  }
                                  

                                  You don't return anything. You should have gotten at least a compiler warning from that.

                                  Anyway, just edit to be:

                                  QByteArray data::get(){
                                      qDebug() << _ba;
                                      return _ba;
                                  }
                                  

                                  And your data.get() will work.

                                  There is no reason to save the data to a file. You're just wasting resources on disk and slowing your app down for no reason.

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

                                  @ambershark

                                  Hi

                                  @ambershark said in Storing data from QnetworkReply:

                                  You don't return anything. You should have gotten at least a compiler warning from that.
                                  Anyway, just edit to be:
                                  QByteArray data::get(){
                                  qDebug() << _ba;
                                  return _ba;
                                  }

                                  Your right about returning _ba. I should have seen that as the function is not void... (my brains been fried working on real project lol), and I agree the compiler should have warned me about that. But that being said unfortunately has not resolved the problem. The output is still ""

                                  However in the same function If I deliberately overwrite _ba, I get output:

                                  QByteArray data::get(){
                                      _ba = "testing...";
                                      qDebug() << _ba;
                                      return _ba;
                                  }
                                  

                                  output: "testing..."

                                  if I remove my call to data::get() in main() and output server data directly from the slot I get my data:

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

                                  There is no reason to save the data to a file. You're just wasting resources on disk and slowing your app down for no reason.

                                  Yep that was the reason for my post

                                  mrjjM J.HilkJ 2 Replies Last reply
                                  0
                                  • L LeeH

                                    @ambershark

                                    Hi

                                    @ambershark said in Storing data from QnetworkReply:

                                    You don't return anything. You should have gotten at least a compiler warning from that.
                                    Anyway, just edit to be:
                                    QByteArray data::get(){
                                    qDebug() << _ba;
                                    return _ba;
                                    }

                                    Your right about returning _ba. I should have seen that as the function is not void... (my brains been fried working on real project lol), and I agree the compiler should have warned me about that. But that being said unfortunately has not resolved the problem. The output is still ""

                                    However in the same function If I deliberately overwrite _ba, I get output:

                                    QByteArray data::get(){
                                        _ba = "testing...";
                                        qDebug() << _ba;
                                        return _ba;
                                    }
                                    

                                    output: "testing..."

                                    if I remove my call to data::get() in main() and output server data directly from the slot I get my data:

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

                                    There is no reason to save the data to a file. You're just wasting resources on disk and slowing your app down for no reason.

                                    Yep that was the reason for my post

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

                                    @LeeH
                                    You did read why it dont work ?
                                    obj.download();
                                    obj.get();
                                    Cannot work as download() is not a blocking call
                                    so you will call obj.get(); BEFORE any data ever is read.

                                    First when you get signal, its safe to use obj.get(); Not before.

                                    1 Reply Last reply
                                    3
                                    • L LeeH

                                      @ambershark

                                      Hi

                                      @ambershark said in Storing data from QnetworkReply:

                                      You don't return anything. You should have gotten at least a compiler warning from that.
                                      Anyway, just edit to be:
                                      QByteArray data::get(){
                                      qDebug() << _ba;
                                      return _ba;
                                      }

                                      Your right about returning _ba. I should have seen that as the function is not void... (my brains been fried working on real project lol), and I agree the compiler should have warned me about that. But that being said unfortunately has not resolved the problem. The output is still ""

                                      However in the same function If I deliberately overwrite _ba, I get output:

                                      QByteArray data::get(){
                                          _ba = "testing...";
                                          qDebug() << _ba;
                                          return _ba;
                                      }
                                      

                                      output: "testing..."

                                      if I remove my call to data::get() in main() and output server data directly from the slot I get my data:

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

                                      There is no reason to save the data to a file. You're just wasting resources on disk and slowing your app down for no reason.

                                      Yep that was the reason for my post

                                      J.HilkJ Offline
                                      J.HilkJ Offline
                                      J.Hilk
                                      Moderators
                                      wrote on last edited by
                                      #42

                                      @LeeH according to what @mrjj said:

                                      QObject::connect(_rep, SIGNAL(readyRead()), this, SLOT(output()));
                                      QObject::connect(_rep, SIGNAL(finished()), this, SLOT(get()));
                                      

                                      should do the trick.

                                      get() will be called as soon as all data is written into the Bytearray.


                                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                      Q: What's that?
                                      A: It's blue light.
                                      Q: What does it do?
                                      A: It turns blue.

                                      1 Reply Last reply
                                      2
                                      • jsulmJ jsulm

                                        @LeeH said in Storing data from QnetworkReply:

                                        Ok... so how should I use QIOdevice here

                                        You use your QNetworkReply which is derived from QIODevice. And you cannot connect a class - you only can connect instances.
                                        Did you read the documentation? http://doc.qt.io/qt-5/qnetworkaccessmanager.html
                                        From there:

                                        QNetworkRequest request;
                                        request.setUrl(QUrl("http://qt-project.org"));
                                        request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
                                        
                                        QNetworkReply *reply = manager->get(request);
                                        connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead())); // THIS ONE
                                        connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
                                                this, SLOT(slotError(QNetworkReply::NetworkError)));
                                        connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
                                                this, SLOT(slotSslErrors(QList<QSslError>)));
                                        
                                        L Offline
                                        L Offline
                                        LeeH
                                        wrote on last edited by LeeH
                                        #43

                                        Hi
                                        I had already modified my code in earlier post. You suggested QIODevice doc for readyRead() but I didn't realise QNetworkReply also had a readRead signal.

                                        @jsulm said in Storing data from QnetworkReply:

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

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

                                          Hi guys

                                          Ah at last it now works! When I did what @mrjj said:

                                          Cannot work as download() is not a blocking call
                                          so you will call obj.get(); BEFORE any data ever is read.

                                          and then what @J.Hilk said after making data::get() a slot:

                                          QObject::connect(_rep, SIGNAL(readyRead()), this, SLOT(output()));
                                          QObject::connect(_rep, SIGNAL(finished()), this, SLOT(get()));
                                          

                                          Success... Thanks so much to EVERYONE that helped!

                                          Lee

                                          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 Extensions
                                          • Unsolved