PDA

View Full Version : Console application - exit application and order of code



Tomasz
21st November 2010, 16:43
Hello!

I've got simple console application and I've got some questions.



#include <QtCore/QCoreApplication>
#include <QTextStream>

QString temp;

int main(int argc, char *argv[])
{
QTextStream out(stdout);
temp = "test";

QCoreApplication a(argc, argv);

out << temp << endl;

return a.exec();
}


First - is the order of code correct? Where should be "QCoreApplication a(argc, argv);"? As i assume "return a.exec();" should be at the end?
What about exiting the application? When I run it in console, it works all the time - don't exits.

thanks in advance
best regards
Tomasz

Lykurg
21st November 2010, 17:42
for that simple project you don't need a QCoreApplication. Simply call
return 0; and your program will exit. (alternatively you can call the quit slot of the applications object.)

As to the order to avoid possible problems in future, put the initialization of the core application at the beginning of the main function.

tbscope
21st November 2010, 18:03
Just a note that you can call the quit slot before a.exec(), but it will not have any effect since the event loop is not running.
Either call quit() after a.exec(), as in create objects that you instantiate in your main function and that use the event loop.
Or use a timer, but then you don't really need the event loop in the first place I guess.