I want to filter some unwanted events sent to my worker QThread
by QCoreApplication::postEvent(...)
, before it actually processed in event loop.
When is event actually filtered by event filter: in postEvent()
in the calling thread or later in QThread
event loop?
I think answer is "in event loop", but i couldn't find answer to that exact question in qt docs.
Qt postEvent() and event filter
2.4k Views Asked by saul_tarvitz At
2
There are 2 best solutions below
0

The filtering must be performed on event delivery, because the filter expects the target object to exist and have an accessible state. Such existence and state can only be guaranteed while the event is being delivered. It is not safe to use QObject
's methods, save a select few, from other threads, thus in general when posting an event, it's not possible to safely access the target object!. It's only possible to do so when the object's lifetime is guaranteed, and thread-safe methods are used to access the state, and it's done in a fashion that doesn't lead to deadlocks.
Both the worker and its event filter need to be living in the same thread. Events are picked up by the thread's event loop and passed through the filter right before being delivered to their receiver
QObject
(when the filter allows this). Here is an example that demonstrates this behavior:Output should look something like: