[Qt-interest] Correct way to manage the main event loop without QApplication::exec()

Andreas Pakulat apaku at gmx.de
Sat Mar 20 11:07:03 CET 2010


On 19.03.10 15:04:41, Nikos Chantziaras wrote:
> On 03/19/2010 02:14 PM, Thiago Macieira wrote:
> > Em Sexta-feira 19 Março 2010, às 12:33:35, Nikos Chantziaras escreveu:
> >> I am using a library in my Qt program that has its own exec()-like
> >> function.  In other words, its own event loop.  That function never
> >> returns, but it calls callbacks I provide.  Those callbacks are the only
> >> way to keep Qt's event loop rolling and to implement the GUI.  Note that
> >> the library in question is not a GUI library and does not process any
> >> system events; it does not perform any kind of system calls.
> >>
> >> Question is, how is this done correctly?  What problems can be expected
> >> if I never call app->exec()?
> >
> > The proper way of doing this is to write your own QAbstractEventDispatcher and
> > handle all Qt requests to register timers and socket notifiers in your 3rd-
> > party library's exec-like function.
> 
> What I'm really asking I guess is what exactly QApplication::exec() is 
> doing to prepare the event loop.

If you want to know what it does, then look up the source code.

>  Right now, I simply call qApp->processEvents() whenever I get the
>  chance.  But I'm doing that without ever having called
>  QApplication::exec().  It does seem to work, but I simply don't know
>  whether there are any complications besides this one:
> 
>    In event you are running a local loop which calls this
>    function continuously, without an event loop, the
>    DeferredDelete  events will not be processed.
> 
> I assume the part that says "without an event loop" means "without ever 
> having called qApp->exec()".

No it means not running an eventloop, there's QEventLoop as separate class
which can be used "standalone" (and thats also done in some places like
QDialog::exec()). You can also pass the necessary flags to processEvents to
also process deferred-deletes.

Andreas

-- 
You will forget that you ever knew me.



More information about the Qt-interest-old mailing list