[Qt-interest] Exiting before the main event loop

Pete Black theblackpeter at gmail.com
Mon Feb 23 19:55:47 CET 2009


Arnold Krille wrote:
> Hi,
> 
> On Monday 23 February 2009 03:56:47 Pete Black wrote:
>> Rohan McGovern wrote:
>>> On Monday 23 February 2009, ext Pete Black wrote:
>>>> Anyone know a good way of exiting an application before the main
>>>> event loop has been entered? That is before app.exec() has been
>>>> called, but withing the app object itself.
>>> Just don't call app.exec() if you don't want to execute the
>>> application.
>> Yeah, was hoping there was a way, but I probably have to do that. Which
>> means refactoring. Boxed myself in a bit by parsing some command line
>> parameters from the app's constructor and I need to be able to quit from
>> there.
> 
> Does that mean that you sub-classes QApplication "just" to parse command line 
> args?

I'm subclassing QApplication, but not to just parse the command line
arguments.

> Why don't you access QApplication::arguments() from your main-function before 
> you run app.exec()? And why can't you skip running app.exec() if your 
> requisites aren't fulfilled. Or use "return" or "exit()" if needed?

That's one way of doing it, and so far the only one unless calling
qFatal() to break out. As I already have a QApplication derived object
and thereby access to the command line arguments one could argue that it
does make sense to keep it there and not move it out of that class.

> I have to admit that I don't really understand your problem...

Forget about command line arguments parsing. How would you, from a
QApplication derived object, exit the application before exec() get called?

int main(int argc, char **argv) {
  MyQApplication app(argc, argv);
  return app.exec();
}

MyQApplication::MyQApplication(int argc, char** argv) :
  QApplication(argc, argv) {

  // want to be able to exit here
}

Now if the answer is that it's bad design and that you're not supposed
to exit from the constructor then fine. But, if redesigning is the only
option then don't you think there should be some other way then just
qFatal() (and perhaps std::exit())?

Thanks,
Pete

> Have fun,
> 
> Arnold
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest




More information about the Qt-interest-old mailing list