PDA

View Full Version : Exiting a Qt Console Application



nbetcher
23rd March 2009, 03:57
Hello,
I am developing a Qt console application and have run into a snag: the application does not quit when told to using 'exit(1);' The class is constructed with QCoreApplication as the parent and within the constructor I parse the command-line parameters passed to the application. If there are no parameters passed or '-h' or '--help' is passed then the application prints the usage help, then quits. Unfortunately the program does not quit after printing the help, then being instructed to exit().

When it prints the help it calls a function to automate that task, and in that function a QTextStream is created against stdout. The help is printed using lines such as this (where 'console' is the QTextStream(stdout) ):


console << "Exit status is 0 if OK, otherwise failure." << endl;

Once the help is printed the function simply ends. It then returns back into the constructor's loop to finish construction and exit(1) is called, but the application doesn't exit. I've also tried 'qApp->exit(1)' to make sure that no funny-business was going on.

Also, the main.cpp file DOES contain 'return a.exec()' where 'a' is the QCoreApplication class constructed.

What is the reason the program is not exiting? Can exit() not be called in the constructor?

Thanks,
Nick Betcher, CPhT

Hiker Hauk
23rd March 2009, 04:32
void QCoreApplication::exit ( int returnCode = 0 ) [static]

Tells the application to exit with a return code.

After this function has been called, the application leaves the main event loop and returns from the call to exec(). The exec() function returns returnCode. If the event loop is not running, this function does nothing.


If you meant to call the exit() in the standard C library, type std::exit.

nbetcher
23rd March 2009, 13:23
void QCoreApplication::exit ( int returnCode = 0 ) [static]

Tells the application to exit with a return code.

After this function has been called, the application leaves the main event loop and returns from the call to exec(). The exec() function returns returnCode. If the event loop is not running, this function does nothing.


If you meant to call the exit() in the standard C library, type std::exit.

Huh? No, for sake of simplicity I just didn't type QCoreApplication::exit() everytime I referred to it. The code has QCoreApplication::exit(1); in the constructor, but the application does not exit. I did not want to call std::exit. Thanks for the help, but the application still does not exit the event loop. :(

Hiker Hauk
23rd March 2009, 15:29
void QCoreApplication::exit ( int returnCode = 0 ) [static]

Tells the application to exit with a return code.

After this function has been called, the application leaves the main event loop and returns from the call to exec(). The exec() function returns returnCode. If the event loop is not running, this function does nothing.


If you meant to call the exit() in the standard C library, type std::exit.

Maybe read again.

nbetcher
23rd March 2009, 21:03
Maybe read again.

Ok. You know what's funny, I actually started to reply to the fact that I still didn't understand my problem, but in typing up the reply I realized what it was. So, as I suspected, I can't call that in the constructor. :) That's what I get for replying this morning to your message... I wasn't awake.

Thanks.