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. QNetworkAccessManager and POST return
QtWS25 Last Chance

QNetworkAccessManager and POST return

Scheduled Pinned Locked Moved Unsolved General and Desktop
qnetworkaccessmpost return
12 Posts 5 Posters 18.0k 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.
  • H Offline
    H Offline
    Helson
    wrote on 15 Dec 2015, 19:00 last edited by Helson
    #1

    Hello,

    networkMgr = new QNetworkAccessManager(this);
    connect(mpNetworkMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinishedValidation(QNetworkReply*)));
    
    mpNetworkMgrSend->post(request, datatoSend);
    
    void MyClass::onFinishedValidation(QNetworkReply *reply)
    {
     	QString data = (QString) reply->readAll(); //It's works!
    }
    

    With this code I below I can't read the response:

    QNetworkReply* reply;
    reply = mpNetworkMgrSend->post(request, datatoSend);
    QString data = (QString) reply->readAll();
    
    

    Why the only way to read the response is inside the slot onFinishedValidation?
    Exist a way to read the response outside of slot or both?

    Thank you!

    J 1 Reply Last reply 16 Dec 2015, 05:28
    0
    • B Offline
      B Offline
      bsomervi
      wrote on 15 Dec 2015, 19:16 last edited by
      #2

      QNetworkAccessManager is designed to work with the Qt event loop. It is event driven and it communications with you also by events. The QNetworkReply::finished() or QNetworkAccessManager::finished(QNetworkReply *) slots are not the only place results can be read, you can also connect the QNetworkReply::readReady() signal and get results incrementally as they stream in.

      H 1 Reply Last reply 15 Dec 2015, 20:17
      1
      • B bsomervi
        15 Dec 2015, 19:16

        QNetworkAccessManager is designed to work with the Qt event loop. It is event driven and it communications with you also by events. The QNetworkReply::finished() or QNetworkAccessManager::finished(QNetworkReply *) slots are not the only place results can be read, you can also connect the QNetworkReply::readReady() signal and get results incrementally as they stream in.

        H Offline
        H Offline
        Helson
        wrote on 15 Dec 2015, 20:17 last edited by
        #3

        @bsomervi
        Thanks for your elucidation.
        if it is not asking too much, you can show an example or something about?

        1 Reply Last reply
        0
        • H Helson
          15 Dec 2015, 19:00

          Hello,

          networkMgr = new QNetworkAccessManager(this);
          connect(mpNetworkMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinishedValidation(QNetworkReply*)));
          
          mpNetworkMgrSend->post(request, datatoSend);
          
          void MyClass::onFinishedValidation(QNetworkReply *reply)
          {
           	QString data = (QString) reply->readAll(); //It's works!
          }
          

          With this code I below I can't read the response:

          QNetworkReply* reply;
          reply = mpNetworkMgrSend->post(request, datatoSend);
          QString data = (QString) reply->readAll();
          
          

          Why the only way to read the response is inside the slot onFinishedValidation?
          Exist a way to read the response outside of slot or both?

          Thank you!

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 16 Dec 2015, 05:28 last edited by
          #4

          @Helson You cannot just read the response right after sending the post. The communication over network takes time. You have to wait until the response arrives and then read it. QNetworkAccessManager::post() just sends the post it does not wait for the response.

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

          H 1 Reply Last reply 16 Dec 2015, 11:20
          1
          • J jsulm
            16 Dec 2015, 05:28

            @Helson You cannot just read the response right after sending the post. The communication over network takes time. You have to wait until the response arrives and then read it. QNetworkAccessManager::post() just sends the post it does not wait for the response.

            H Offline
            H Offline
            Helson
            wrote on 16 Dec 2015, 11:20 last edited by Helson
            #5

            @jsulm
            It's clear for me now.
            Can you show me a example of a response outside of a slot?
            I need it to check the response and decide the way the code
            If I use slot, I need put all my logic inside of a slot.

            Thank you and sorry for my bad english.

            --
            Something like

            reply = mpNetworkMgrSend->post(request, datatoSend);

            if(reply = "true"){
            ...
            } else{
            ...
            }

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 16 Dec 2015, 11:51 last edited by
              #6

              You get a pointer to a QNetworkReply instance.
              Then you connect its readyRead() signal (or finished()) to a slot where you read the response data.
              Where is no need to do busy waiting for the reply.
              This is from Qt documentation (replace get() with post()):

              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()));
              connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
                      this, SLOT(slotError(QNetworkReply::NetworkError)));
              

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

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 16 Dec 2015, 12:04 last edited by
                #7

                If you want to wait for the response for some reason you can call the QNetworkReply::isFinished() periodically. But take care to not to block the event loop, else you most probably will have an endless loop.

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

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  Helson
                  wrote on 16 Dec 2015, 16:41 last edited by Helson
                  #8

                  I'm trying but without success.

                  QNetworkReply *reply = manager->get(request);
                  connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
                  connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError)))
                   
                  while(!reply->isFinished()){                         
                      QString dataReply = (QString) reply->readAll();	
                  }
                  

                  I can not get the reply out of the slot...

                  1 Reply Last reply
                  0
                  • speqtrS Offline
                    speqtrS Offline
                    speqtr
                    wrote on 16 Dec 2015, 16:51 last edited by
                    #9

                    Try this code:

                    QNetworkReply *reply = manager->get(request);
                    
                    QEventLoop loop;
                    connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
                    loop.exec();
                    
                    QByteArray data = reply->readAll();
                    QString dataReply(data);
                    

                    It should do the job.

                    H 1 Reply Last reply 16 Dec 2015, 16:57
                    3
                    • speqtrS speqtr
                      16 Dec 2015, 16:51

                      Try this code:

                      QNetworkReply *reply = manager->get(request);
                      
                      QEventLoop loop;
                      connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
                      loop.exec();
                      
                      QByteArray data = reply->readAll();
                      QString dataReply(data);
                      

                      It should do the job.

                      H Offline
                      H Offline
                      Helson
                      wrote on 16 Dec 2015, 16:57 last edited by Helson
                      #10

                      @speqtr

                      Hey!!
                      It's works like a charm!
                      Have a great day!
                      Thank you so much!!!

                      Problem solved finally!!!

                      1 Reply Last reply
                      0
                      • speqtrS Offline
                        speqtrS Offline
                        speqtr
                        wrote on 16 Dec 2015, 17:22 last edited by
                        #11

                        You are welcome!
                        Have a nice day :)

                        HeerokNewbieH 1 Reply Last reply 26 Dec 2021, 06:35
                        3
                        • speqtrS speqtr
                          16 Dec 2015, 17:22

                          You are welcome!
                          Have a nice day :)

                          HeerokNewbieH Offline
                          HeerokNewbieH Offline
                          HeerokNewbie
                          wrote on 26 Dec 2021, 06:35 last edited by
                          #12
                          This post is deleted!
                          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