[Interest] Event handling for QObjects created before QApplication

Till Oliver Knoll till.oliver.knoll at gmail.com
Mon Sep 22 10:11:01 CEST 2014


Am 22.09.2014 um 02:46 schrieb Thiago Macieira <thiago.macieira at intel.com>:

> On Monday 22 September 2014 04:24:21 Dmitrii Volosnykh wrote:
>> Threads and QObjects
>> <http://qt-project.org/doc/qt-5/threads-qobject.html> tutorial
>> 
>> says:
>>> Note that for QObjects that are created before QApplication,
>>> QObject::thread() returns zero. This means that the main thread will only
>>> handle posted events for these objects; other event processing is not done
>>> at all for objects with no thread.
>> 
>> Could someone elaborate on this, please? The most confusing part is "main
>> thread will only handle posted events for these objects".
> 
> Qt is not supported before the creation of QCoreApplication. Your use-case is 
> not supported, so no answer is necessary.
> 
> Just don't do it.
> 
> Note: the doc is wrong.

It is always a good idea - if not even a hard pre-condition for most or even all QObject based classes - to first create a Q(Core|Gui)Application instance before anything else.

Besides that, I understand "This means that the main thread will only handle posted events for these objects" as "events directly sent (posted) with that specific QObject as target (in contrast to other events such as system events with no particular QObject as target, e.g. keyboard or mouse events).

So the doc writer must have had either QCoreApplication::postEvent or sendEvent in mind (which both let you specify the QObject receiver). However the former requires the main event loop running, which is not the case when you have not yet created a QCoreApplication instance - and we're back at the start.

So in theory it could mean that (only) events sent by QCoreApplication::sendEvent are getting through to your QObject, even without a QCoreApplication instance being around.

In practise I'd always follow Thiago's advice.

I agree that the doc should get some review in either case.

Cheers,
  Oliver




More information about the Interest mailing list