PDA

View Full Version : Exit button that does a task before exiting



Roelof
11th September 2009, 11:32
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

DrDonut
11th September 2009, 11:50
You can try to write a destructor. This is automaticly called when you try to close your program and it would look like this:



MyClass::~MyClass()
{
//your code...
}


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

Good luck!

yogeshgokul
11th September 2009, 11:51
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.

drhex
11th September 2009, 11:52
Connect clicked() to you own slot that calls your "few functions" and ends with calling quit(). (Slots can be called like ordinary methods)

Roelof
11th September 2009, 13:44
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?

DrDonut
11th September 2009, 15:21
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!