Results 1 to 7 of 7

Thread: execute sth before program exits (user closes window)

  1. #1
    Join Date
    Jul 2008
    Posts
    66
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default execute sth before program exits (user closes window)

    I need to write on a device sth like "bye bye" before my program exits. (User closes the window).

    How can I do that?
    There must be a signal which is sent before destroying all the instances of the app.
    I must be sure that no instances of my objects are destroyed, so I can write on the device.

    After writing my "bye bye" message on the device, the app can be closed (every instances of my objects can be destroyed).

  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: execute sth before program exits (user closes window)

    You can either reimplement closeEvent of any of your windows, reimplement any of the destructors, reimplement the application and save the data in its destructor or save the data after QApplication::exec() returns in main() - the choice is yours.

  3. #3
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: execute sth before program exits (user closes window)

    Quote Originally Posted by wysota View Post
    You can either reimplement closeEvent of any of your windows, reimplement any of the destructors, reimplement the application and save the data in its destructor or save the data after QApplication::exec() returns in main() - the choice is yours.
    Wow, what an exhaustive list. I guess the only thing you didn't mention is the QCoreApplication::aboutToQuit() signal At least that's what I use for this purpose.

  4. #4
    Join Date
    Jul 2008
    Posts
    66
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: execute sth before program exits (user closes window)

    Thanks both I will test all of your suggestions.

    I thought about
    Qt Code:
    1. myapp::closeEvent(..... *event)
    2. {
    3. emit sendcmd(); //here I write my "bye bye" on the device
    4. closePort(); // here I close the device
    5. event->accept() // here i finally exit the program
    6. }
    To copy to clipboard, switch view to plain text mode 

    I found that this was not a good idea because:
    Before I could write sth on the device "closePort" was called....

    I think I should make the "emit sendcmd()" direct connected?? Right?
    It will return when all the stuff is finished, so closePort can be executed.

    Any other ideas to do that?
    I think there is not "return value" for signals which i can check if I can proceed or not.

  5. #5
    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: execute sth before program exits (user closes window)

    What exactly are you trying to achieve anyway? If you explain it to us, it'll be easier to suggest some concrete solution.

  6. #6
    Join Date
    Jul 2008
    Posts
    66
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: execute sth before program exits (user closes window)

    I try to inform the remote side that my application will be closed and cannot receive anymore.

    Instead of forcing the user to click on a "disconnect" button i would do this internal before exiting the program.

    It must happen after pressing Alt+F4 or clicking on X and before all the destructors of my objects are called. The whole functionality of the program must still exist in this small time window.

  7. #7
    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: execute sth before program exits (user closes window)

    In that case using QCoreApplication::aboutToQuit() or spinning a local event loop after QCoreApplication::exec() returns should suit your situation fine.

    Remember that closing a window doesn't destroy it so you can still do everything with its objects until you call delete on it or the object runs out of scope.

Similar Threads

  1. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 04:19

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.