Exit button that does a task before exiting
Good day.
I'm trying to create a exit button that executes a few functions before closing. Therefore I can not use
connect(exitButton,SIGNAL(clicked()),this, SLOT(quit()));
since this close the program directly.
Does anyone know how to write a customized quit() function or something else that can be used to execute a few functions and then close the handle of the program.
Any help would greatly be appreciated
thanx
Re: Exit button that does a task before exiting
You can try to write a destructor. This is automaticly called when you try to close your program and it would look like this:
Code:
MyClass::~MyClass()
{
//your code...
}
This way your code is automaticly executed right before the program closes.
Good luck!
Re: Exit button that does a task before exiting
1. You can do your desired in closeEvent.
2. you can connect the exit button with your own slot. Do whatever you want, at the end call exit() function in that slot.
3. Do same in destructor.
Re: Exit button that does a task before exiting
Connect clicked() to you own slot that calls your "few functions" and ends with calling quit(). (Slots can be called like ordinary methods)
Re: Exit button that does a task before exiting
Thanx for the help. I forgot about destructors.
DrDonut how would you define the destructor in a class? I tried the following:
class Controller : public QWidget
{
Q_OBJECT
public:
Controller(QWidget *parent = 0);
~Controller();
but receives the following error:
error LNK2019: unresolved external symbol "public: void __thiscall Controller::exit(void)" (?exit@Controller@@QAEXXZ) referenced in function "public: virtual int __thiscall Controller::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Controller@@UAEHW4Call@QMetaObject@@ HPAPAX@Z) moc_CONTROLLER.obj
Do you have any idea?
Re: Exit button that does a task before exiting
Hi Roelof,
As far as I know, it should work the way you put it in your header file.
Did you moc your header file in the proper way? (That would explain the metaObject stuff in the error message).
Good luck!