Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. The right way to do something before closing the program - closeEvent, aboutToQuit or the Destructor
Forum Updated to NodeBB v4.3 + New Features

The right way to do something before closing the program - closeEvent, aboutToQuit or the Destructor

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 4.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fs_tigre
    wrote on last edited by
    #1

    Hi,

    I need to save an int before the application closes. I have been reading about it and I found out that this can be done in a few different ways and I'm wondering what would be the best way to accomplish what I want.

    Do I use the QDialog::closeEvent, the aboutToQuit signal or I simply add my code to the destructor?

    What would be the main difference between these three different methods?

    I again, all I want to do is to be able to save an int that will be extracted from QLabel right before the application exits.

    Thanks a lot

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on last edited by
      #2

      It depends ;-)

      QCoreApplication::aboutToQuit will be emitted when your application is about to quit, so this happens exactly once. The closeEvent of your QDialog is called when your dialog closes. It can happen many times, because the same dialog may be shown many times while the application is running. Finally, the destructor of your QDialog is executed when the dialog object gets deleted. Depending on how your application is designed, the destructor may be called many times (for example when your create a new instance of the QDialog every time you show it, and you delete that instance after the dialog was closed). If you create only a single instance of your QDialog (and maybe show that one instance many times), the destructor is only executed once.

      Personally, I would keep the destructor for "clean up" tasks that correspond to the constructor, e.g. the destructor is the correct place for deleting instances you have created in the constructor. I'd tend to save the data when the dialog is closed, in closeEvent() or maybe even use the QDialog::accept() signal - so it gets only saved when the user clicked the "okay" button.

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fs_tigre
        wrote on last edited by
        #3

        Thanks a lot for the good explanation. I will go with the closeEvent().

        Thanks

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved