PDA

View Full Version : How to close application?



beerkg
9th August 2006, 18:50
Hi, I know this seems to be a stupid question, but I really have a problem. I've got this code:

void Form1::openDialog(){
Dialog *dialog=new Dialog(this);
int result=dialog->exec();

if(result==QDialog::Rejected){
//here I want to close the application
}

delete dialog;
}

Form1 inherits from QMainWindow

I've tried almost everything (qApp->quit(),close()) and nothing works. Maybe someone knows what to do.

jacek
9th August 2006, 18:58
I've tried almost everything (qApp->quit(),close()) and nothing works.
qApp->quit() should work, unless you invoke openDialog() in a loop or you block the event loop in some way.

beerkg
10th August 2006, 13:32
Sorry I dont quite understand: "unless you invoke openDialog() in a loop". You mean the dialog loop or main application loop? I want to quit after the dialog loop is finished. One thing i've noticed is when I use QMessageBox before qApp->quit() it worked. It looks like Dialog doesnt have enough time to finish its event loop before I use qApp->quit()

jacek
10th August 2006, 14:37
You mean the dialog loop or main application loop?
I meant something like:
while( 1 ) {
dlg.exec();
}


I want to quit after the dialog loop is finished. One thing i've noticed is when I use QMessageBox before qApp->quit() it worked. It looks like Dialog doesnt have enough time to finish its event loop before I use qApp->quit()
So qApp->quit() doesn't work or it works too fast?

beerkg
10th August 2006, 16:31
When I do something like that:


void Form1::openDialog(){
Dialog *dialog=new Dialog(this);
int result=dialog->exec();

if(result==QDialog::Rejected){
QMessageBox::Information(this,"","");
qApp->quit();
}

delete dialog;
}


it works fine, but without this meeage box it doesn't work.

3dch
10th August 2006, 19:26
Hi

From the Qt docs:

Regarding qApp->quit(), qApp->exit() etc:
Note that ... this function does return to the caller -- it is event processing that stops.


Therefore try this: First terminate the Qt event loop processing and then end the application with the exit() function from stdlib.h:



#include <QCoreApplication>
#include <stdlib.h>

...
if(result == QDialog::Rejected)
{
QCoreApplication::quit(0);
exit(0);
}


Regards
Ernst