You can try to write a destructor. This is automaticly called when you try to close your program and it would look like this:

Qt Code:
  1. MyClass::~MyClass()
  2. {
  3. //your code...
  4. }
To copy to clipboard, switch view to plain text mode 

This way your code is automaticly executed right before the program closes.

Good luck!