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.0k 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.
  • L LeeH
    24 Jul 2017, 21:55

    @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();
    
    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 25 Jul 2017, 04:41 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 25 Jul 2017, 13:50
    2
    • J jsulm
      25 Jul 2017, 04:41

      @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 25 Jul 2017, 13:50 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

      J 1 Reply Last reply 25 Jul 2017, 13:57
      0
      • L LeeH
        25 Jul 2017, 13:50

        @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

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 25 Jul 2017, 13:57 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 25 Jul 2017, 14:28
        1
        • J jsulm
          25 Jul 2017, 13:57

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

          L Offline
          L Offline
          LeeH
          wrote on 25 Jul 2017, 14:28 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 J 2 Replies Last reply 25 Jul 2017, 14:36
          0
          • L LeeH
            25 Jul 2017, 14:28

            @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 Offline
            M Offline
            m.sue
            wrote on 25 Jul 2017, 14:36 last edited by
            #24

            Hi @LeeH

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

            -Michael.

            L 1 Reply Last reply 25 Jul 2017, 14:51
            0
            • M m.sue
              25 Jul 2017, 14:36

              Hi @LeeH

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

              -Michael.

              L Offline
              L Offline
              LeeH
              wrote on 25 Jul 2017, 14:51 last edited by
              #25

              @m.sue

              Hi,

              QIODevice is an abstract class.

              M 1 Reply Last reply 25 Jul 2017, 15:10
              0
              • L LeeH
                25 Jul 2017, 14:51

                @m.sue

                Hi,

                QIODevice is an abstract class.

                M Offline
                M Offline
                m.sue
                wrote on 25 Jul 2017, 15:10 last edited by
                #26

                Hi @LeeH

                then the object of the derived class that you use.

                -Michael.

                L 1 Reply Last reply 25 Jul 2017, 16:16
                0
                • M m.sue
                  25 Jul 2017, 15:10

                  Hi @LeeH

                  then the object of the derived class that you use.

                  -Michael.

                  L Offline
                  L Offline
                  LeeH
                  wrote on 25 Jul 2017, 16:16 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 1 Reply Last reply 25 Jul 2017, 16:26
                  0
                  • L LeeH
                    25 Jul 2017, 16:16

                    @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 Offline
                    M Offline
                    m.sue
                    wrote on 25 Jul 2017, 16:26 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 25 Jul 2017, 19:16
                    0
                    • M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 25 Jul 2017, 17:02 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 25 Jul 2017, 19:22
                      0
                      • M m.sue
                        25 Jul 2017, 16:26

                        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 25 Jul 2017, 19:16 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
                        • M mrjj
                          25 Jul 2017, 17:02

                          @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 25 Jul 2017, 19:22 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()

                          M 1 Reply Last reply 25 Jul 2017, 19:27
                          0
                          • L LeeH
                            25 Jul 2017, 19:22

                            @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()

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 25 Jul 2017, 19:27 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 25 Jul 2017, 19:52
                            0
                            • M mrjj
                              25 Jul 2017, 19:27

                              @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 25 Jul 2017, 19:52 last edited by
                              #33

                              @mrjj

                              When I added:

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

                              i got:

                              size 2634
                              

                              Yes data::output() is definately being called

                              M 1 Reply Last reply 25 Jul 2017, 19:57
                              0
                              • L LeeH
                                25 Jul 2017, 19:52

                                @mrjj

                                When I added:

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

                                i got:

                                size 2634
                                

                                Yes data::output() is definately being called

                                M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 25 Jul 2017, 19:57 last edited by
                                #34

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

                                L 1 Reply Last reply 25 Jul 2017, 20:02
                                0
                                • M mrjj
                                  25 Jul 2017, 19:57

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

                                  L Offline
                                  L Offline
                                  LeeH
                                  wrote on 25 Jul 2017, 20:02 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 25 Jul 2017, 20:27
                                  0
                                  • L LeeH
                                    25 Jul 2017, 20:02

                                    @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 25 Jul 2017, 20:27 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 26 Jul 2017, 04:09
                                    0
                                    • L LeeH
                                      25 Jul 2017, 20:27

                                      @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 26 Jul 2017, 04:09 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 26 Jul 2017, 09:57
                                      2
                                      • L LeeH
                                        25 Jul 2017, 14:28

                                        @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

                                        J Offline
                                        J Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on 26 Jul 2017, 04:13 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 26 Jul 2017, 10:10
                                        1
                                        • M Offline
                                          M Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on 26 Jul 2017, 09:33 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

                                          29/45

                                          25 Jul 2017, 17:02

                                          • Login

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