What's the QMetaCallEvent for and how to access its details?
Asked Answered
E

3

6

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?

Eta answered 4/6, 2012 at 19:31 Comment(0)
R
10

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 use QMetaObject::invoke or QMetaObject::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:

  1. You provide Qt::QueuedConnection argument to QObject::connect or QMetaObject::invoke[Method], or

  2. The 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 sender QObject 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:

#include <private/qobject_p.h> // Declaration of QMetaCallEvent

bool Object::eventFilter(QObject * watched, QEvent * event) {
  QLabel * label = qobject_cast<QLabel*>(watched);
  if (! label || event->type() != QEvent::MetaCall) return false;
  QMetaCallEvent * mev = static_cast<QMetaCallEvent*>(event);
  static int setNumIdx = QLabel::staticMetaObject.indexOfSlot("setNum(int)");
  if (mev->id() != setNumIdx) return false;
  int num = *reinterpret_cast<int*>(mev->args()[1]);
  // At this point, we can invoke setNum ourselves and discard the event
  label->setNum(num);
  return true;
}

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.

template <class Base> class MetaCallWatcher : public Base {
  MetaCallWatcher(int& argc, char** argv) : Base(argc, argv) {}
  bool notify(QObject * receiver, QEvent * event) { 
    if (event->type() == QEvent::MetaCall) {
      QMetaCallEvent * mev = static_cast<QMetaCallEvent*>(event);
      QMetaMethod slot = receiver->metaObject()->method(mev->id());
      qDebug() << "Metacall:" << receiver << slot.methodSignature();
    }
    return Base::notify(receiver, event);
  }
}

int main(int argc, char ** argv) {
  MetaCallWatcher<QApplication> app(argc, argv);
  ...
}
Recommendatory answered 8/1, 2014 at 0:9 Comment(5)
I can not include private/qobject_p.h, compiler doensn't see it.Cutout
@Cutout You must have a full installation of Qt done from sources. In your case, the sources aren't present, so the private headers aren't present either.Staceestacey
This works as long as you use the old string based signal/slot connections. With function pointers, QMetaCallEvent.id() does not contain a helpful information anymore. This is the best I could squeeze out of the new signal/slot connections: gist.github.com/webmaster128/2d4df2e46818d4e76fb7f7b8940c841aFlorri
For some reason, I have to include this path: #include <QtCore/5.6.0/QtCore/private/qobject_p.h> to get the "private" headers. I have a suspition this is intentional to force me to include particular Qt version due to aforementioned compatibility issues.Urdu
No, you don't have to do that. Add QT += core_private to the .pro file, and #include <private/qobject_p.h>, then delete the build folder of your project and rebuild. It will work. There's never a question of what version you're using: the current one you build the project with. Feel free to look for this include in any of my stackoverflow answers on github, it works just fine.Staceestacey
B
0

The QEvent::MetaCall-type event is created whenever a signal has been emitted that is connected to a slot in the receiving QObject. 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.

Bigname answered 12/6, 2012 at 8:26 Comment(2)
Thank you for the information. To accept an answer, I would still like to know what it is used for, perhaps in a real-world example, and if there is anything available from the QEvent type.Eta
It is false that the QMetaCallEvent is created whenever a connected signal is emitted. It would make the signal-slot mechanism much slower than necessary.Staceestacey
D
-2

QEvent::MetaCall is used for delivering cross-thread signals.

Destiny answered 20/11, 2013 at 6:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.