PDA

View Full Version : Quit, Exit qApp (program) if error?



Arsenic
21st July 2008, 21:55
I am trying to figure out how to properly exit and destroy all my classes if say an error happens.

Trying to make like a close the application if this message box pops up.

Doing this:

qApp->exec();
return;

Works, but it leaves the Process in Task Manager alive just the window doesn't show up.

JimDaniel
21st July 2008, 22:21
exec() starts the event loop, its called in main normally.

You probably want qApp->quit();

Arsenic
21st July 2008, 22:24
If I use quit(), the error message comes up and then the program starts up anyway. It is suppose to quit the whole program and destroy everything.

I mean how do people put like a File > Exit ?? What do they connect() to "exit"?

JimDaniel
21st July 2008, 23:40
Your situation sounds to me like a situation I've had before, where you need to do a test in the main window/widget constructor, and if it fails you need to exit the program, but as this is being called before app.exec() in main(), it doesn't work properly. My solution was a hack, but it should work.


QTimer::singleShot(250, qApp, SLOT(quit()));

Probably someone here with more experience should step in and suggest a more correct solution. I would like to know myself.

Arsenic
22nd July 2008, 00:20
Thanks, it seems to work. I think my main problem was I was trying to use it in my class (QMainWindow):

I tried
qApp->quit();

that didn't do anything.

I tried
close(); (because QMainWindow has a close function)
but apparently, your singleShot timer works awesome, thanks I had forgotten about that.

aamer4yu
22nd July 2008, 06:12
Just a thought -...
Wont it be better if we use some kind of factory for the objects ??
The factory may return a valid object if the parameters meet the condition, or else it returns null.

JimDaniel
22nd July 2008, 23:16
Its been bothering me I suggested so clearly a hackish method of solving the problem. I realized today a better solution. Not sure why it didn't occur to me before:



int main(int argc, char * argv[])
{
QApplication app(argc, argv);
QWidget myWidget;

bool passed_test = myWidget.testForSomething();
if(passed_test)
{
myWidget.show();
return app.exec();
}
else
{
return 1;
}
}

jpn
27th July 2008, 09:27
Calling QApplication::quit() indeed doesn't have any effect if one later calls QApplication::exec().

MarkoSan
30th September 2008, 08:54
Well, I have following chunk of code:
{
// sets up database paramaters
m_Database=QSqlDatabase::addDatabase(db_type);
m_Database.setHostName(db_host);
m_Database.setUserName(db_username);
m_Database.setPassword(db_password);
m_Database.setDatabaseName(db_name);
// tries to connect to database
m_bConnectionEstablished=m_Database.open();
if(!m_bConnectionEstablished)
{
//qFatal("Failed to connect to database. Aborting.");
/*
int ret=QMessageBox::critical(0,
tr("Critical Error"),
tr("Failed to connect to database. Aborting."));
*/
QString errorString(m_Database.lastError().databaseText()) ;

errorString.append(": ");
errorString.append(m_Database.lastError().driverTe xt());
int ret=QMessageBox::critical(0,
tr("Critical Error"),
errorString);

qApp->quit(); // terminates application, does not work?!
}

What I want to achieve, if database connection fails, application must terminate immediately. qApp->quit() does not work, are there any other ideas?

jpn
30th September 2008, 10:36
When does this chunk of code get executed? Before entering to the event loop (ie. before calling QCoreApplication::exec())?

MarkoSan
30th September 2008, 11:02
Here is code:
eROSystem w;
w.showMaximized();
pIntroWindow->finish(&w); // sets splash window closing time
delete pIntroWindow; // deletes pIntroWindow
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();

Database code is executed in following line:
eROSystem w;. This is the first line of this code chunk.

jpn
30th September 2008, 11:36
So, first in the eROSystem constructor you call QCoreApplication::quit() and then later in main() you call QCoreApplication::exec(). That won't make the application quit. First you tell it to exit the event loop, then you tell it to enter the event loop.

MarkoSan
30th September 2008, 12:21
Well, how do I then terminate application?!

jpn
30th September 2008, 12:59
Get rid of all that logic in eROSystem constructor. Constructors are supposed to initialize objects but that's all. Don't write too much logic into constructors or you'll have problems like this. Consider moving the logic to another method:


MyObject obj;
...
if (obj.doSomething(...))
return app.exec(); // enter to the event loop _only_ if applicable