I'm trying to understand the whole internal process of Qt and how it works when I'm working with different threads.
As I've understood (googling and exploring the Qt source code), is as following:
- Each thread has a local "pending event list" and a local event loop (if I call to
exec
) that interacts with that list. QCoreApplication::postEvent(obj, e)
appends the pair(obj, e)
on the "pending event list" of theobj
's thread.- Each thread has a local "event dispatcher" (
QAbstractEventDispatcher
specializations), which purpose is reading system events. So, it exists aQEventDispatchWin
, aQEventDispatchUnix
, aQEventDispatchSymbian
and so on, for different platforms. Forgui
events, Qt has alsoQEventDispatchX11
(inherits fromQEventDispatchUnix
),S60
(fromSymbian
), etc.
With of all this in mind, a exec
call works as following:
Thread's `exec`:
├ create a QEventLoop object.
└ call QEventLoop.exec()
└ call repeatedly eventDispatcher's processEvents with WaitForMoreEvents flag.
├ call to QCoreApplication::sendPostedEvents
├ while (!pending system events)
│ ├ read system event
│ ├ create an appropiate QEvent e and detect its target QObject o.
│ └ call to QCoreApplication::sendSpontaneousEvent(o, e)
└ call to QCoreApplication::sendPostedEvents
(for new generated user events in the previous step).
If quit
or exit
is called, it finalices the current processEvents
call and exec
returns with the value passed to exit
.
Some points to take in consideration:
- System events are never pushed/posted: when they are generated from the system and translated as QEvents, they are directly sended to its target object.
- Target object member functions (
o.event()
) are called in the same thread whereprocessEvent
takes place.
And now, doubts:
- Since
postEvent
is a static and thread-safe function, what role does QCoreApplication play in this event processing system? And QApplication? Why are they mandatory to being created as soon as possible? - Why QApplication/QCoreApplication are mandatory to get system events, if each Thread has its own "event dispatcher"?
Any correction about my supositions are welcome.