Results 1 to 8 of 8

Thread: How to confirm the clean exit of QApplication

  1. #1
    Join Date
    Oct 2013
    Location
    Everett WA USA
    Posts
    12
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default How to confirm the clean exit of QApplication

    Dear experts,


    I am working on one project where I use Qt for GUI development. Qt is running at the main thread. I would like to confirm the clean exit of the QApplication. This is required to do some post application exit operation such as running exit handlers.

    When the application needs to exited I use,

    qApp->quit();

    to confirm the successful exit of the QApplication, I use

    if ( true == qApp->closingDown())
    {
    //Successfull exit of the QApplication. Do post exit operations
    }


    Questions:
    a. Does qApp->quit() immediately makes qApp->closingDown() function to return the true value.
    b. Are there any other way to confirm the successful exit of the QApplication?

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

    Default Re: How to confirm the clean exit of QApplication

    You can cleanup after QApplication::exec() returns before returning from main().

    Qt Code:
    1. int main(...) {
    2. QApplication app(...);
    3.  
    4. int ret = app.exec();
    5. doCleanup();
    6. return ret;
    7. }
    To copy to clipboard, switch view to plain text mode 
    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
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,484
    Thanked 276 Times in 271 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to confirm the clean exit of QApplication

    How about QCoreApplication::aboutToQuit signal ?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to confirm the clean exit of QApplication

    I guess it mosly depends on whether you need the event loop during cleanup.
    If you do, Lesiok's suggestion would give you that, if you don't, I'd also go for Wysota's suggestion.

    Cheers,
    _

  5. #5
    Join Date
    Oct 2013
    Location
    Everett WA USA
    Posts
    12
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to confirm the clean exit of QApplication

    Thanks for the response.

    I want to do the cleanup activities post the QApplication exit. The QApplication documentation says that
    "We recommend that you connect clean-up code to the aboutToQuit() signal, instead of putting it in your application's main() function. This is because, on some platforms the QApplication::exec() call may not return."
    QApplication.

    Can
    void qAddPostRoutine(QtCleanUpFunction ptr)
    helps to confirm the clean exit of the application and do the cleanup activities?


    Added after 19 minutes:


    In our application, We need to call the "exit(someExitCode)" from different thread which inturn runs the registered exit handlers for cleanup activities.
    To call exit() fromother thread, other thread must be sure that QApplication is exited ( event loop is exited) and no more events are processed in Qt event loop.

    So does qAppPostRoutine helps in this regard? Or are there any best way for my problem context?

    Thanking You

    Regards
    Last edited by sraju; 18th August 2014 at 17:49.

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

    Default Re: How to confirm the clean exit of QApplication

    Well, if you happen to be on a platform where exec() does not return, then main() doesn't exit either, so the QApplication does not go out of scope, thus is not deleted, thus its destructor is not invoked and cannot call then post routines.

    Cheers,
    _

  7. #7
    Join Date
    Oct 2013
    Location
    Everett WA USA
    Posts
    12
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to confirm the clean exit of QApplication

    @ands_skoa Yes you are right. Post routines cannot be executed if QApplication::exec() doesn't return. The platform in which I am working returns from exec.

    As a sloution, I am thinking to use QCoreApplication::exit(withSuccessExitCode) ( instead of qApp->quit()) to ensure the successful exit of the QApplication so that I can happily perform post routine activities.

    Kindly post your opinion.
    Last edited by sraju; 18th August 2014 at 22:46. Reason: missed statements

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

    Default Re: How to confirm the clean exit of QApplication

    Quote Originally Posted by sraju View Post
    @ands_skoa Yes you are right. Post routines cannot be executed if QApplication::exec() doesn't return. The platform in which I am working returns from exec.

    As a sloution, I am thinking to use QCoreApplication::exit(withSuccessExitCode) ( instead of qApp->quit()) to ensure the successful exit of the QApplication so that I can happily perform post routine activities.
    Basically calling quit() or exit() are equivalent. The docs of quit() say:

    Quote Originally Posted by docs
    Tells the application to exit with return code 0 (success). Equivalent to calling QCoreApplication::exit(0).
    I think you are spending much too much time on thinking about this. It seems that whatever solution you choose that launches after event processing stops will be ok for you. There is no "best" approach here so why spend so much time trying to discover what the "best" solution is?
    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. A problem about "undefined reference to `Clean::Clean(QObject*)'
    By GeorgeDao123 in forum Qt for Embedded and Mobile
    Replies: 4
    Last Post: 10th February 2013, 09:36
  2. Replies: 2
    Last Post: 27th July 2011, 04:24
  3. How works QApplication::exit()
    By anoraxis in forum Newbie
    Replies: 5
    Last Post: 12th April 2011, 20:13
  4. Clean Exit of QWSServer
    By ssc in forum Qt Programming
    Replies: 0
    Last Post: 11th January 2011, 12:55
  5. clean exit!!
    By Raajesh in forum Qt Programming
    Replies: 1
    Last Post: 17th June 2008, 16:33

Tags for this Thread

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.