Results 1 to 9 of 9

Thread: instead of Qeventloop which method uset to wait for response

  1. #1
    Join Date
    Nov 2014
    Location
    Chennai
    Posts
    160
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    65

    Default instead of Qeventloop which method uset to wait for response

    here below code i used QEventloop for waiting for response,,instead of QEventloop is there any method to wait for response ,can any one give me suggestion for this
    Thanks in advance
    Qt Code:
    1. QNetworkAccessManager manager;
    2. QNetworkReply *response = manager.get(QNetworkRequest(QUrl(url)));
    3. QEventLoop event;
    4. connect(response,SIGNAL(finished()),&event,SLOT(quit()));
    5. event.exec();
    6. QString html = response->readAll();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanked 97 Times in 94 Posts

    Default Re: instead of Qeventloop which method uset to wait for response

    That is the way to go if you want to wait for the reply to finish, although I am not sure if the finished() signal is always guaranteed to be emitted in case of error.

    Unless this synchronous approach greatly simplifies the design of your application, I suggest you follow the asynchronous approach and partition your code into the part sending the request and the part handling the reply. E.g.

    Qt Code:
    1. void MyObject::sendRequest(/* ... */) {
    2. // Send the request
    3. manager.get(QNetworkRequest(QUrl(url)));
    4. connect(&manager, SIGNAL(finished(QNetworkReply *)), SLOT(handleReply(QNetworkReply *)));
    5. }
    6.  
    7. void MyObject::handleReply(QNetworkReply *reply) {
    8. // Handle the reply here
    9. QString html = reply->readAll();
    10. // ...
    11. }
    To copy to clipboard, switch view to plain text mode 

    Last but not least, you have not connected to readyRead(), which means that the reply may never emit finished() if it is large, and your event loop will run forever.

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

    iswaryasenthilkumar (24th April 2015)

  4. #3
    Join Date
    Nov 2014
    Location
    Chennai
    Posts
    160
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    65

    Default Re: instead of Qeventloop which method uset to wait for response

    i need to wait for response,,i used your code but i got networkstatus=0,,while using eventloop my statuscode getting proper result.i should wait for response instead of using qeventloop.please help me
    Quote Originally Posted by yeye_olive View Post
    That is the way to go if you want to wait for the reply to finish, although I am not sure if the finished() signal is always guaranteed to be emitted in case of error.

    Unless this synchronous approach greatly simplifies the design of your application, I suggest you follow the asynchronous approach and partition your code into the part sending the request and the part handling the reply. E.g.

    Qt Code:
    1. void MyObject::sendRequest(/* ... */) {
    2. // Send the request
    3. manager.get(QNetworkRequest(QUrl(url)));
    4. connect(&manager, SIGNAL(finished(QNetworkReply *)), SLOT(handleReply(QNetworkReply *)));
    5. }
    6.  
    7. void MyObject::handleReply(QNetworkReply *reply) {
    8. // Handle the reply here
    9. QString html = reply->readAll();
    10. // ...
    11. }
    To copy to clipboard, switch view to plain text mode 

    Last but not least, you have not connected to readyRead(), which means that the reply may never emit finished() if it is large, and your event loop will run forever.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: instead of Qeventloop which method uset to wait for response

    What is wrong with using QEventLoop?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: instead of Qeventloop which method uset to wait for response

    Quote Originally Posted by iswaryasenthilkumar View Post
    i need to wait for response,,i used your code but i got networkstatus=0
    Where do you get that?

    Cheers,
    _

  7. #6
    Join Date
    Nov 2014
    Location
    Chennai
    Posts
    160
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    65

    Default Re: instead of Qeventloop which method uset to wait for response

    while using QEventloop it wait for until i got response no other event will execute,,thats the problem,Thats why i asked is there any method to waiting process instead of QEventloop?

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: instead of Qeventloop which method uset to wait for response

    Quote Originally Posted by iswaryasenthilkumar View Post
    while using QEventloop it wait for until i got response no other event will execute
    That's not true.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #8
    Join Date
    Nov 2014
    Location
    Chennai
    Posts
    160
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    65

    Default Re: instead of Qeventloop which method uset to wait for response

    i havee doubt in this code can you explain please
    Qt Code:
    1. QUrl url1(pic);
    2. QNetworkReply *response = manager.get(QNetworkRequest(QUrl(url1)));
    3. QEventLoop event;
    4. connect(response,SIGNAL(finished()),&event,SLOT(quit()));//this eventloop will quit untill it reaches finished,,if suppose we not get response from mangaer what will happen here,,event will still in waiting process to quit am i correct.
    5. event.exec();
    To copy to clipboard, switch view to plain text mode 
    Quote Originally Posted by wysota View Post
    That's not true.

  10. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: instead of Qeventloop which method uset to wait for response

    Why are you going back to using an event loop after yeye_olive explained how you can do it without, the very question that is still the subject of this thread?

    And yes, an event loop continues its execution until its quit() method is called.

    Cheers,
    _

Similar Threads

  1. Problems with QEventLoop
    By marnando in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 25th February 2014, 19:19
  2. Wait Condition Indication to User Using Loading/Wait Message
    By vivek.panchal in forum Qt Programming
    Replies: 4
    Last Post: 14th August 2012, 16:40
  3. Replies: 1
    Last Post: 12th October 2010, 22:38
  4. wait for method to complete?
    By jtdavidson in forum Newbie
    Replies: 1
    Last Post: 18th July 2010, 07:16
  5. How to use QEventLoop?
    By MorrisLiang in forum Newbie
    Replies: 3
    Last Post: 13th May 2010, 16:23

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
  •  
Qt is a trademark of The Qt Company.