PDA

View Full Version : How works QApplication::exit()



anoraxis
8th April 2011, 17:37
On my application I try to retrieve data from a database. If this step is succesfull the application continues, Instead I want that the application shows a QMessage::critical(...) and closes with QApplication::exit(). The error appears but the application dosn't stops. The code is:


wMain::wMain(QWidget *parent) : QWidget(parent), ui(new Ui::wMain)
{
ui->setupUi(this);
this->actualizarInterfaz();
}

void wMain::actualizarInterfaz()
{
DAO* aDao = new DAO("");
this->asg = aDao->Query(1);
delete aDao;

if(this->asg != NULL)
{
// populate gui with data recovered
}
else
{
QMessageBox::critical(
this,
"Error",
"...");
qApp->exit(1);
}
}

How could I fix this? There are another ways to do it?

wysota
8th April 2011, 17:40
What exactly do you mean that the application "doesn't stop"? Did you read about differences between QCoreApplication::exit() and ::exit()?

viulskiez
10th April 2011, 14:23
I agree with wysota. In this case, I'm just figure how qApp->exit() called. I guess that your problem is calling QApplication::exit() before entering the application main loop. In the other word, QApplication::exit() called before QApplication::exec() executed. So if you want to stop your application before QApplication entering the main loop, call ::exit() that provided by stdlib.h.

xtothat
12th April 2011, 19:58
I discovered another way to quit your Qt app before the main event loop has started a couple of days ago. I needed to quit the application within the constructor of the main window, and it seems that the following does the job:


QMetaObject::invokeMethod(this, "close", Qt::QueuedConnection);

X-T

viulskiez
12th April 2011, 20:49
I discovered another way to quit your Qt app before the main event loop has started.
That's not TRUE, in fact the application event loop has started and the Widget costructed, show, then closed by method invocation. You are misunderstand with Qt:QueuedConnection. Do you actually know what you've done?

wysota
12th April 2011, 21:13
If you want to quit the application then just return from main.