Results 1 to 9 of 9

Thread: qApp->processEvents();

  1. #1
    Join Date
    Jun 2007
    Location
    italy
    Posts
    126
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question qApp->processEvents();

    Hi Guys,

    With the following Qt Code I am trying to send an QHttp request and get the related
    response, doing this turning the asynchronous operation into a synchronous one,
    without using signals and slots..

    Here the code:
    Qt Code:
    1. http = new QHttp(this);
    2. ....
    3. int id = http->request(header, byteArray);
    4.  
    5. while(http->currentId() != 0)
    6. {
    7. qApp->processEvents();
    8. }
    9.  
    10. QString result = http->readAll();
    To copy to clipboard, switch view to plain text mode 

    In my PC I get the result correctly, in other ones the Gui seems to freeze and the application don't get on..

    I am wandering if the "qApp->processEvents();" instruction is the problem and how i can avoid this behaviour..

    Using
    Qt Code:
    1. while(http->currentId() != 0)
    2. {
    3. QCoreApplication::processEvents();
    4. }
    To copy to clipboard, switch view to plain text mode 

    would be the same?

    If so, what about the fallowing method ??
    Qt Code:
    1. QNetworkAccessManager manager;
    2. QTimer tT;
    3.  
    4. tT.setSingleShot(true);
    5. connect(&tT, SIGNAL(timeout()), &q, SLOT(quit()));
    6. connect(&manager, SIGNAL(finished(QNetworkReply*)),
    7. &q, SLOT(quit()));
    8. QNetworkReply *reply = manager.get(QNetworkRequest(
    9. QUrl("http://www.qtcentre.org")));
    10.  
    11. tT.start(5000); // 5s timeout
    12. q.exec();
    13.  
    14. if(tT.isActive()){
    15. // download complete
    16. tT.stop();
    17. } else {
    18. // timeout
    19. }
    To copy to clipboard, switch view to plain text mode 

    How to adapt it to perform my task (sending request and waiting for its response before going on with the flow) ???


    Many thanks in advance

    Roby

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

    Default Re: qApp->processEvents();

    Use the second method, it's beautiful
    Your code from the first one is incorrect, anyway. You might employ a local event loop to make it work, but it's a bad idea.

    How to use the code? Well... after the exec() call you will either have your reply in the reply object ready to read or a timeout will have occured. The "if" block is there to check which one occured. Then simply do your stuff.
    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.


  3. #3
    Join Date
    Jun 2007
    Location
    italy
    Posts
    126
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qApp->processEvents();

    many thanks Wysota..

    Just to better understand, you mean that the code that follows
    Qt Code:
    1. while(http->currentId() != 0)
    2. {
    3. QCoreApplication::processEvents();
    4. }
    To copy to clipboard, switch view to plain text mode 
    is also incorrect ??

    Then, about the second method, I would like you to suggest how to modify the first part of code. I well understand the "if block".

    Is it correct like this ?
    Qt Code:
    1. QHttp *http;
    2. QTimer tT;
    3.  
    4. tT.setSingleShot(true);
    5. connect(&tT, SIGNAL(timeout()), &q, SLOT(quit()));
    6. connect(http, SIGNAL(finished(QNetworkReply*)),&q, SLOT(quit()));
    7.  
    8. //**********************
    9. id = http->request(header, b);
    10. //**********************
    11.  
    12. tT.start(5000); // 5s timeout
    13. q.exec();
    14.  
    15. if(tT.isActive()){
    16. //*****************
    17. result = http->readAll();
    18. //*****************
    19. tT.stop();
    20. } else {
    21. // timeout
    22. }
    To copy to clipboard, switch view to plain text mode 

    Many thanks in advance

    Roby

  4. #4
    Join Date
    Jun 2007
    Location
    italy
    Posts
    126
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qApp->processEvents();

    When I use the code:
    Qt Code:
    1. QTimer tT;
    2.  
    3. tT.setSingleShot(true);
    4. connect(&tT, SIGNAL(timeout()), &q, SLOT(quit()));
    5. connect(http, SIGNAL(done(QHttp*)),&q, SLOT(quit()));
    6.  
    7. //**********************
    8. id = http->request(header, b);
    9. //**********************
    10.  
    11. tT.start(5000); // 5s timeout
    12. q.exec();
    13.  
    14. if(tT.isActive()){
    15. //*****************
    16. result = http->readAll();
    17. //*****************
    18. tT.stop();
    19. } else {
    20. // timeout
    21. }
    To copy to clipboard, switch view to plain text mode 

    I can't get to the "if" block, then result is always empty..

    What wrongs ??

    Roby

  5. #5
    Join Date
    Jun 2007
    Location
    italy
    Posts
    126
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qApp->processEvents();

    OK Wysota

    I made a mistake with the SLOT signal, which is done(bool) in case of http->request..

    SO, the code:
    Qt Code:
    1. QTimer tT;
    2.  
    3. tT.setSingleShot(true);
    4. connect(&tT, SIGNAL(timeout()), &q, SLOT(quit()));
    5. connect(http, SIGNAL(done(bool)),&q, SLOT(quit()));
    6.  
    7. //**********************
    8. id = http->request(header, b);
    9. //**********************
    10.  
    11. tT.start(600000); // 6minuto timeout
    12. q.exec();
    13.  
    14. if(tT.isActive()){
    15. //*****************
    16. result = http->readAll();
    17. //*****************
    18. tT.stop();
    19. } else {
    20. QMessageBox::warning(this, "TIME OUT", ("time out"));
    21. // timeout
    22. }
    To copy to clipboard, switch view to plain text mode 

    Seem to be ok..

    What's your opinin?
    Many thanks in advance..

    Roby

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

    Default Re: qApp->processEvents();

    Quote Originally Posted by rmagro View Post
    Just to better understand, you mean that the code that follows
    Qt Code:
    1. while(http->currentId() != 0)
    2. {
    3. QCoreApplication::processEvents();
    4. }
    To copy to clipboard, switch view to plain text mode 
    is also incorrect ??
    No, I mean the loop is incorrect, it would exit too soon.

    Is it correct like this ?
    Qt Code:
    1. QHttp *http;
    2. QTimer tT;
    3.  
    4. tT.setSingleShot(true);
    5. connect(&tT, SIGNAL(timeout()), &q, SLOT(quit()));
    To copy to clipboard, switch view to plain text mode 
    Why use QHttp at all if you have a better mechanism available? What's wrong with QNetworkAccessManager?
    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.


  7. #7
    Join Date
    Jun 2007
    Location
    italy
    Posts
    126
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: qApp->processEvents();

    Ok ...I can try to adapt my code to use QNetworkAccessManager
    but do you thing something is wrong with the code that follows:
    Qt Code:
    1. QTimer tT;
    2.  
    3. tT.setSingleShot(true);
    4. connect(&tT, SIGNAL(timeout()), &q, SLOT(quit()));
    5. connect(http, SIGNAL(done(bool)),&q, SLOT(quit()));
    6.  
    7. //**********************
    8. id = http->request(header, b);
    9. //**********************
    10.  
    11. tT.start(600000); // 6minuto timeout
    12. q.exec();
    13.  
    14. if(tT.isActive()){
    15. //*****************
    16. result = http->readAll();
    17. //*****************
    18. tT.stop();
    19. } else {
    20. QMessageBox::warning(this, "TIME OUT", ("time out"));
    21. // timeout
    22. }
    To copy to clipboard, switch view to plain text mode 

    Thanks

  8. #8
    Join Date
    Jun 2007
    Location
    italy
    Posts
    126
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qApp->processEvents();

    Any suggestion to adapt the fallowing code to use the QNetworkAccessManager Class ??

    Qt Code:
    1. QTimer tT;
    2.  
    3. tT.setSingleShot(true);
    4. connect(&tT, SIGNAL(timeout()), &q, SLOT(quit()));
    5. connect(http, SIGNAL(done(bool)),&q, SLOT(quit()));
    6.  
    7. //**********************
    8. id = http->request(header, b);
    9. //**********************
    10.  
    11. tT.start(5000); 5s
    12. q.exec();
    13.  
    14. if(tT.isActive()){
    15. //*****************
    16. result = http->readAll();
    17. //*****************
    18. tT.stop();
    19. } else {
    20. QMessageBox::warning(this, "TIME OUT", ("time out"));
    21. // timeout
    22. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: qApp->processEvents();

    Quote Originally Posted by rmagro View Post
    Ok ...I can try to adapt my code to use QNetworkAccessManager
    but do you thing something is wrong with the code that follows:
    Yes, I do. There is no way to properly read the incoming data.


    Quote Originally Posted by rmagro View Post
    Any suggestion to adapt the fallowing code to use the QNetworkAccessManager Class ??
    There is nothing to adapt, just take the original code using QNetworkAccessManager.
    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.


Similar Threads

  1. Qt-based dll: qApp = 0 !!
    By Elder Orb in forum Qt Programming
    Replies: 6
    Last Post: 14th September 2010, 16:15
  2. Quit, Exit qApp (program) if error?
    By Arsenic in forum Newbie
    Replies: 13
    Last Post: 30th September 2008, 12:59
  3. ohhh my, Getting handle to main app
    By sticcino in forum Qt Programming
    Replies: 6
    Last Post: 20th June 2008, 21:02
  4. how to use qApp inside a dll?
    By oob2 in forum Qt Programming
    Replies: 1
    Last Post: 23rd June 2006, 22:15
  5. Finding QObjcects in a qApp
    By rianquinn in forum Qt Programming
    Replies: 4
    Last Post: 6th February 2006, 12:16

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.