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

John McClurkin jwm at nei.nih.gov
Fri Jun 26 20:13:41 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?

One thing to consider. The first argument to QApplication is a reference 
to argc, not a copy. Therefore, if QApplication changes argc, it will be 
changed in main. Looking at the source code, there is one place where it 
could be changed. Therefore, I would make local copies of argc and argv 
and initialize them inside the do-while loop and then pass these copies 
  when creating a QApplication object.

> 
> 
> Thanks in advance,
> 
> diego



More information about the Qt-interest-old mailing list