Results 1 to 5 of 5

Thread: Properly ending background tasks on qApp->quit()

  1. #1
    Join Date
    May 2014
    Posts
    6
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Properly ending background tasks on qApp->quit()

    When closing a program, it runs the following code block

    Qt Code:
    1. connect(ui->actionExit, &QAction::triggered, [&]() {
    2. s.setValue("mainWindowGeometry", saveGeometry());
    3. s.setValue("mainWindowState", saveState());
    4.  
    5. qApp->quit();
    6. });
    To copy to clipboard, switch view to plain text mode 

    but sometimes the application will still hang around in the background and I have to kill it through task manager.
    I suspect that this is related to various QtConcurrent::run() and QEventLoop::exec() calls. If I use exit(0) or qApp->exit(0) rather than qApp->quit() the application seems to exit more frequently but it still ends up hanging.

    Is there a better way to approach exiting an application that will prevent it from hanging around in the background?

  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: Properly ending background tasks on qApp->quit()

    All quit() does is that it exits the event loop. I believe it should exit all QEventLoop::exec() calls too, so that shouldn't be a problem. As for Qt Concurrent, you probably have some registry of scheduled tasks so you can prevent quitting the event loop before all of them are finished or you can cancel them before you quit.
    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. The following user says thank you to wysota for this useful post:

    matt4682 (1st April 2015)

  4. #3
    Join Date
    May 2014
    Posts
    6
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Properly ending background tasks on qApp->quit()

    Aha, I started keeping a registry by appending the returns of QtConcurrent::run() to a QList<QFuture<void>> then when I try to exit out of the program I can just do

    Qt Code:
    1. for(QFuture<void> f : this->registry) {
    2. if(f.isRunning()) {
    3. qApp->processEvents();
    4. f.waitForFinished();
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 
    Before the application calls qApp->quit().
    Since the QFutures were made from QtConcurrent::run() they can't be cancelled so I just set a bool to signal the running functions to finish up

  5. #4
    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: Properly ending background tasks on qApp->quit()

    Just a general remark: it is often more appropriate to do that window geometry saving in the window's closeEvent() handler and have the exit/quit action connected to the window's close() slot.

    That way the saving is also invoked if the window is closed through the windowing system, e.g. the user clicking the window decoration's X button, ALT-F4, the user closing the window through context menu of the taskbar, etc.

    Cheers,
    _

  6. #5
    Join Date
    May 2014
    Posts
    6
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Properly ending background tasks on qApp->quit()

    Quote Originally Posted by anda_skoa View Post
    Just a general remark: it is often more appropriate to do that window geometry saving in the window's closeEvent() handler and have the exit/quit action connected to the window's close() slot.

    That way the saving is also invoked if the window is closed through the windowing system, e.g. the user clicking the window decoration's X button, ALT-F4, the user closing the window through context menu of the taskbar, etc.

    Cheers,
    _
    The application has a close to tray setting so more often than not, the close event is ignored.
    I ended up moving the entire exit procedure into its' own method and it's called from various places in the program now (tray icon exit, menu exit and window close if close to tray is disabled).

Similar Threads

  1. Eventloop does not quit properly
    By MrAnderson1983 in forum Qt Programming
    Replies: 7
    Last Post: 4th June 2014, 10:31
  2. qApp->quit() can not work
    By xstream71 in forum Qt Programming
    Replies: 2
    Last Post: 15th October 2013, 08:56
  3. qApp->quit() deletes all its children?
    By zgulser in forum Qt Programming
    Replies: 1
    Last Post: 7th April 2012, 23:32
  4. Replies: 2
    Last Post: 1st August 2011, 06:30
  5. Quit, Exit qApp (program) if error?
    By Arsenic in forum Newbie
    Replies: 13
    Last Post: 30th September 2008, 12:59

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.