I have an event filter and I noticed when I click to expand/collapse a tree branch I get QEvent::MetaCall
. I was thinking about using this to roll my own expand/collapse code, but I don't know how to get any information, such as the index of the item.
Is there anything available from this MetaCall event type?
I found someone had asked this same question on another site, but without an answer, here: http://www.qtcentre.org/threads/37525-How-to-filter-QEvent-MetaCall
What is this event typically used for?
QEvent::MetaCall
is used for delivering cross-thread signals.The
QEvent::MetaCall
-type event is created whenever a signal has been emitted that is connected to a slot in the receivingQObject
. Reacting to this event in a custom filter/event handler seems to circumvent Qt's mightiest feature, the signal-slot architecture. It's probably better to find out which slot is called and if that slot is virtual so you can overload it.The biggest question are: What exactly are you trying to do? What is the Qt class that received those events? As far as I'm concerned, you're trying to do things the hard way, so why bother?
The
QMetaCallEvent
is the event representing a slot call whenever a queued connection is used to invoke a slot. This might be due to a signal firing that was connected to a slot, or due to the useQMetaObject::invoke
orQMetaObject::invokeMethod
. The queued connection bit is the important part! Queued connections are not used by default for calls between objects in the same thread, since they have the event queue management overhead, unless either of the two conditions below holds true:You provide
Qt::QueuedConnection
argument toQObject::connect
orQMetaObject::invoke[Method]
, orThe receiving object's
thread()
is different from the thread where the call is originating - at the time of the call.The
QMetaCallEvent
event class carries the information needed to invoke a slot. It contains the senderQObject
and its signal id (if the call comes from a signal-slot connection), as well as the target slot identifier, and the arguments needed to be passed into the slot.Thus, you could check if the called slot is the one you wish to intercept, as well as what arguments were passed to it. For example, if you're calling a slot with a single
int
parameter, then*reinterpret_cast<int*>(metaCallEvent->args()[1])
will give you the value of that integer. The zero-th argument is used for the return value, if any, so the parameters are indexed with base 1.Disclaimer Since the
QMetaCallEvent
class is internal to Qt's implementation, you're making your application's binary tied to the particular Qt version in full (entire major.minor version) and you lose the benefits of binary compatibility offered by Qt across the major version. Your code may still compile but cease to work properly when you switch to another minor version of Qt!The below applies to Qt 5.2.0, I have not looked at any other versions!
So, suppose you want to intercept a call to
QLabel::setNum
. You'd catch such events as follows:If you want to see, globally, all slots that are called using the metacall system, you can do that too. Template parametrization of the base class allows flexibility to use any application class - say
QCoreApplication
,QGuiApplication
,QApplication
, or a user-derived type.