QMetaCallEvent 的用途是什么以及如何访问其详细信息? [英] What's the QMetaCallEvent for and how to access its details?

查看:17
本文介绍了QMetaCallEvent 的用途是什么以及如何访问其详细信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个事件过滤器,我注意到当我点击展开/折叠一个树枝时,我得到 QEvent::MetaCall.我正在考虑使用它来滚动我自己的展开/折叠代码,但我不知道如何获取任何信息,例如项目的索引.

这种 MetaCall 事件类型有什么可用的吗?

我发现有人在另一个网站上问过同样的问题,但没有答案,在这里:http://www.qtcentre.org/threads/37525-How-to-filter-QEvent-MetaCall

此事件通常用于什么?

解决方案

最大的问题是:你到底想做什么?接收这些事件的 Qt 类是什么? 就我而言,您正在努力以艰难的方式做事,所以何必呢?

QMetaCallEvent 是表示每当使用 排队连接 调用槽时槽调用的事件.这可能是由于连接到插槽的信号触发,或由于使用了 QMetaObject::invokeQMetaObject::invokeMethod.排队连接位是重要的部分!默认情况下,队列连接用于同一线程中的对象之间的调用,因为它们具有事件队列管理开销,除非以下两个条件之一成立:

  1. 您向 QObject::connectQMetaObject::invoke[Method] 提供 Qt::QueuedConnection 参数,

  2. 接收对象的 thread() 与发起调用的线程不同 - 在调用时.

QMetaCallEvent 事件类携带调用插槽所需的信息.它包含发送方 QObject 及其信号 ID(如果调用来自信号槽连接),以及目标槽标识符,以及需要传递到槽中的参数.

因此,您可以检查被调用的插槽是否是您希望拦截的插槽,以及传递给它的参数.例如,如果您使用单个 int 参数调用插槽,则 *reinterpret_cast(metaCallEvent->args()[1])会给你那个整数的值.第零个参数用于返回值(如果有),因此参数以基数 1 进行索引.

免责声明 由于 QMetaCallEvent 类是 Qt 实现的内部类,因此您将应用程序的二进制文件完全绑定到特定的 Qt 版本(整个major.minor 版本)并且您失去了 Qt 在主要版本中提供的二进制兼容性的好处.当您切换到 Qt 的另一个次要版本时,您的代码可能仍然可以编译但停止正常工作!

以下适用于Qt 5.2.0,其他版本我没看过!

因此,假设您想拦截对 QLabel::setNum 的调用.您会按如下方式捕获此类事件:

#include //QMetaCallEvent 的声明bool Object::eventFilter(QObject * 观看,QEvent * 事件) {QLabel * label = qobject_cast(观看);if (!label || event->type() != QEvent::MetaCall) return false;QMetaCallEvent * mev = static_cast(事件);static int setNumIdx = QLabel::staticMetaObject.indexOfSlot("setNum(int)");如果 (mev->id() != setNumIdx) 返回 false;int num = *reinterpret_cast(mev->args()[1]);//此时,我们可以自己调用setNum,丢弃该事件标签-> setNum(num);返回真;}

如果您想在全局范围内查看使用元调用系统调用的所有插槽,您也可以这样做.基类的模板参数化允许灵活地使用任何应用程序类 - 例如 QCoreApplicationQGuiApplicationQApplication 或用户派生的类型.

template 类MetaCallWatcher:公共基地{MetaCallWatcher(int& argc, char** argv) : Base(argc, argv) {}bool 通知(QObject * 接收器,QEvent * 事件){if (event->type() == QEvent::MetaCall) {QMetaCallEvent * mev = static_cast(事件);QMetaMethod slot =receiver->metaObject()->method(mev->id());qDebug() <<元调用:"<<接收器<<slot.methodSignature();}返回 Base::notify(receiver, event);}}int main(int argc, char ** argv) {MetaCallWatcher<QApplication>应用程序(argc,argv);...}

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?

解决方案

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);
  ...
}

这篇关于QMetaCallEvent 的用途是什么以及如何访问其详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆