Results 1 to 6 of 6

Thread: Seriously Noob question: QCoreApplication::exec hangs

  1. #1
    Join Date
    Aug 2012
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Seriously Noob question: QCoreApplication::exec hangs

    Would appreciate someone's help with this newbie question.

    With Qt 4.7, 4.8 and 5.0, my console application hangs when I run it. I'm expecting it to run to completion.

    Qt Code:
    1. // from app.h
    2. class App : public QObject
    3. {
    4. Q_OBJECT
    5. public:
    6. explicit App(QCoreApplication *app, QObject *parent = 0);
    7.  
    8. void exec()
    9. {
    10. emit finished();
    11. }
    12.  
    13. signals:
    14. void finished();
    15.  
    16. public slots:
    17.  
    18. };
    19.  
    20. // from app.cpp
    21. App::App(QCoreApplication *app, QObject *parent) :
    22. QObject(parent)
    23. {
    24. connect(this, SIGNAL(finished()), app, SLOT(quit()));
    25. }
    26.  
    27. // from main.cpp
    28. int main(int argc, char *argv[])
    29. {
    30. QCoreApplication a(argc, argv);
    31.  
    32. App app(&a);
    33. app.exec();
    34.  
    35. return a.exec();
    36. }
    To copy to clipboard, switch view to plain text mode 

    Why doesn't this app just immediately terminate and return control to the command line?

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

    Default Re: Seriously Noob question: QCoreApplication::exec hangs

    Quote Originally Posted by jbarnesweb View Post
    Why doesn't this app just immediately terminate and return control to the command line?
    Qt Code:
    1. a.exec()
    To copy to clipboard, switch view to plain text mode 
    runs the Qt event loop. This does not return until quit() or exit() are called on the same object.
    Since you program doesn't call either after event loop processing has started, it will continue to do event processing forever.

    Cheers,
    _

  3. #3
    Join Date
    Aug 2012
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: Seriously Noob question: QCoreApplication::exec hangs

    Ok. Next question. Since a.exec() blocks, how do I call a.quit() after a.exec()?

  4. #4
    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: Seriously Noob question: QCoreApplication::exec hangs

    From another object, ui or a timer.

    The point here, however, is that you have two separate calls to exec(), only the first of which is terminated.

    If you really want an application that exits immediately then don't call exec() at all, or connect a zero length timer to qApp quit()

    The exec() function is not designed to be overridden (not virtual)

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

    Default Re: Seriously Noob question: QCoreApplication::exec hangs

    Quote Originally Posted by ChrisW67 View Post
    The point here, however, is that you have two separate calls to exec(), only the first of which is terminated.
    Actually only once, the first exec() is just a normal C++ method on QObject derived class "App".
    Got me confused as well

    It is basically just a no-op, it emits a signal, which calls quit() but since the application is not running yet it doesn't do anything.

    Cheers,
    _

  6. #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: Seriously Noob question: QCoreApplication::exec hangs

    I stand corrected. My brain saw "class App", assumed it sub-classed QCoreApplication, and then refused to read differently

Similar Threads

  1. How to get Text value of a QDomElement ? (noob question)
    By BillGates in forum Qt Programming
    Replies: 6
    Last Post: 31st August 2010, 14:01
  2. (noob question) write qint64 into qsharedmemory
    By daemonna in forum Qt Programming
    Replies: 1
    Last Post: 28th June 2010, 11:28
  3. QWizard noob question
    By Ossi in forum Newbie
    Replies: 5
    Last Post: 2nd September 2009, 12:30
  4. query on QCoreApplication::exec
    By psadhukhan in forum Qt Programming
    Replies: 1
    Last Post: 2nd December 2008, 11:33
  5. a simple noob question
    By wrproject in forum Newbie
    Replies: 1
    Last Post: 1st July 2008, 23:25

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.