[Qt-interest] Exiting before the main event loop

Arnold Krille arnold at arnoldarts.de
Mon Feb 23 20:39:31 CET 2009


Hi,

no need to CC me, I read the list (especially the topics I responded to).

On Monday 23 February 2009 19:55:47 Pete Black wrote:
> Arnold Krille wrote:
> > 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())?

Actually the constructor is "only" to create and initialize your object. If 
you encounter a problem leaving your object in a state where the eventloop 
shouldn't run, it may be that you need a state-machine.

But several other solutions come to mind:

1) Subclass exec() too. Or create your own myexec() which only calls exec if 
your requisites are fulfilled.

2) Create a "bool MyQApplication::shouldRun()" and replace the app.exec()-line 
in your main with "if ( app.shouldRun() ) app.exec()".

3) Don't subclass QApplication but do all the needed stuff in main()...

Have fun,

Arnold
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090223/3fdaa54c/attachment.bin 


More information about the Qt-interest-old mailing list