Results 1 to 6 of 6

Thread: How to wait until the request has finished and then process the reply within one fn

  1. #1
    Join Date
    May 2011
    Posts
    120
    Thanks
    33
    Qt products
    Qt4
    Platforms
    Windows

    Default How to wait until the request has finished and then process the reply within one fn

    hai,

    Qt Code:
    1. void HttpPost::on_pushButton_clicked()
    2. {
    3. params.addQueryItem("testSample","indu");
    4. postData = params.encodedQuery();
    5. manager->setCookieJar(new QNetworkCookieJar(manager));
    6. connect (manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinish(QNetworkReply*)));
    7. manager->post(req, postData);
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. void HttpPost::replyFinish(QNetworkReply* reply)
    2. {
    3.  
    4. QString answer = QString::fromUtf8(reply->readAll());
    5. qDebug() << "answer------------>"<<answer;
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 


    This code is working for me but i don't want to use the "connect" here. I want the reply in the same function. I used
    if (reply->isFinished()==true) but it doesnt work
    How to wait until the request has finished and then process the reply within one function

  2. #2
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to wait until the request has finished and then process the reply within one

    Catch finished() signal!!! which is being called after complete download

  3. The following user says thank you to Gokulnathvc for this useful post:

    vinayaka (15th December 2011)

  4. #3
    Join Date
    May 2011
    Posts
    120
    Thanks
    33
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to wait until the request has finished and then process the reply within one

    Can you please give some more details or code as i needed it now to add it to my project.

  5. #4
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to wait until the request has finished and then process the reply within one

    You need to use isbytesavailable() signal, in order to know the status of the download. And then check for the finished() signal. it will be called once the download is completed.

  6. The following user says thank you to Gokulnathvc for this useful post:

    vinayaka (15th December 2011)

  7. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to wait until the request has finished and then process the reply within one

    This code is working for me but i don't want to use the "connect" here. I want the reply in the same function.
    Why ? What's wrong with the connect() ?
    Anyway, to answer your question, you will have to block the processing somehow. It's not tested, but maybe you can
    1) busy waiting with processEvents calls:
    Qt Code:
    1. void HttpPost::on_pushButton_clicked()
    2.  
    3. {
    4. params.addQueryItem("testSample","indu");
    5. postData = params.encodedQuery();
    6. manager->setCookieJar(new QNetworkCookieJar(manager));
    7. QNetworkReply * reply = manager->post(req, postData);
    8. while( ! reply->isFinished() ){
    9. qApp->processEvents();
    10. }
    11. // reply should be finished by now
    12. }
    To copy to clipboard, switch view to plain text mode 
    2) use QDialog (or QProgressDialog)
    Qt Code:
    1. void HttpPost::on_pushButton_clicked()
    2.  
    3. {
    4. params.addQueryItem("testSample","indu");
    5. postData = params.encodedQuery();
    6. manager->setCookieJar(new QNetworkCookieJar(manager));
    7. QDialog dialog; dialog.setWindowTitle("Waiting...");
    8. // ... prepare the dialog layout somehow
    9. connect (manager, SIGNAL(finished(QNetworkReply *)), &dialog, SLOT(accept()));
    10. QNetworkReply * reply = manager->post(req, postData);
    11. dialog.exec();
    12. // reply should be finished by now, but not if you close the dialog manually
    13. }
    To copy to clipboard, switch view to plain text mode 
    Again, this code is not tested.

  8. The following user says thank you to stampede for this useful post:

    vinayaka (15th December 2011)

  9. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to wait until the request has finished and then process the reply within one

    If you really must have a blocking behaviour then you can use QEventLoop, connect the reply finished() signal to the quit() slot of the loop, and call exec(). When loop.exec() returns you need to check for errors before assuming the request worked. You might also want to implement a timeout in case the request hangs for an extended period.

    On another note... much of the setup you have in the on_pushButton_clicked() function probably should be elsewhere (constructor maybe). You probably do not want to create a new connection to a finished() slot and new cookie jar every time the button is pushed.

  10. The following user says thank you to ChrisW67 for this useful post:

    vinayaka (15th December 2011)

Similar Threads

  1. Receiving reply from page
    By Trok in forum Qt Programming
    Replies: 3
    Last Post: 29th September 2011, 12:31
  2. Request ID of QNetworkaccessmanager get and post request
    By dineshkumar in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2011, 21:56
  3. Qhttp::request(...) method and GET request
    By mikhailt in forum Qt Programming
    Replies: 4
    Last Post: 15th September 2006, 12:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.