[Qt-interest] Returning arbitrary value with QApplication::exit()

Girish Ramakrishnan girish at forwardbias.in
Sat Jun 27 06:08:31 CEST 2009


Diego Schulz wrote:
> Hi all,
> 
> I'm wondering if it's correct to return an arbitrary value with qApp->exit().
> It works as I expect, but I'm not sure if it's completely correcto to
> do what I do.
> 
> I'll try to explain a bit,
> 
> Suppose a main.cpp like this
> 
> 
> #include <QtGui/QApplication>
> #include "mainwindow.h"
> #include "common.h"
> 
> int main(int argc, char *argv[])
> {
>    int retcode=0;
> 
>    do {
>     QApplication a(argc, argv);
>     MainWindow w;
>     retcode = a.exec();
> 
>    } while (retcode == RESTART_APPLICATION);
> 
>   return retcode;
> }
> 
> 
> RESTART_APPLICATION is defined in common.h  as
> 
>    #define RESTART_APPLICATION 333
> 
> 333 is whatever, an arbitrary number.
> 
> In a few circumstances, I want the application to be restarted (eg
> database connection lost, etc), so I call
> 
>    qApp->exit(RESTART_APPLICATION);
> 
> 
> Is it correct to use exit()  in this way? Is there something I should
> be aware if I want to do this?
> 

It is not wrong but I will not recommend this. If your db connection is
lost, why not just reconnect? Or if you want to make it simple, destroy
all your db/connection objects and start afresh. Why do you need to
destroy QApplication itself?

The reason to not use it is, as Andreas Pakulat, said is that you will
find probably find hard to track errors. 'probably' is the keyword,
since no on does it this way, it is unlikely all this is well tested by
Qt users or developers. Besides QApplication has very many static
functions and atleast it used to rely on lots of static data. For your
code to work, the QApplication code must init/uninit correctly. Since
the the process exits immediately after qApp is destroyed no one notices
if things were uninited correctly.

Again, I am not saying it is wrong, just that if you want to 'get things
done asap', I won't recommend it.

Girish



More information about the Qt-interest-old mailing list