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

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

问题描述

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



此MetaCall事件类型有什么可用吗?



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



这是什么事件通常用于?

解决方案

最大的问题是:你想要做什么?什么是接收这些事件的Qt课程?就我而言,您正在尝试以艰苦的方式做事情,所以为什么要麻烦?



QMetaCallEvent 是每当使用排队连接来调用插槽时,代表插槽调用的事件。这可能是由于连接到插槽的信号触发,或者由于使用 QMetaObject :: invoke QMetaObject :: invokeMethod 。排队的连接位是重要的部分!排队的连接默认情况下使用,因为它们具有事件队列管理开销,除非下列两个条件之一都成立:


  1. 您提供 Qt :: QueuedConnection 参数 QObject :: connect QMetaObject :: invoke [Method]


  2. 接收对象的 thread()不同于呼叫发起的线程。


QMetaCallEvent 事件类携带调用插槽所需的信息。它包含发件人 QObject 及其信号id(如果来自信号槽连接的呼叫)以及目标槽标识符以及需要传递的参数进入插槽。



因此,您可以检查被调用的插槽是否是要拦截的插槽,以及传递给它的参数。例如,如果您使用单个 int 参数调用插槽,则 * reinterpret_cast< int *>(metaCallEvent-> args )[1])将给你该整数的值。第0个参数用于返回值(如果有),因此参数将以基数1编制索引。



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



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



所以,假设你想截取一个调用$ code> QLabel :: setNum 。你会收到如下事件:

  #include< private / qobject_p.h> // QMetaCallEvent的声明

bool Object :: eventFilter(QObject *观看,QEvent *事件){
QLabel * label = qobject_cast< QLabel *>(观看);
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]);
//此时,我们可以自己调用setNum并放弃事件
label-> setNum(num);
返回true;
}

如果要全局查看使用metacall调用的所有插槽系统,你也可以这样做。基类的模板参数化可以灵活地使用任何应用程序类 - 例如 QCoreApplication QGuiApplication QApplication 或用户派生类型。

 模板< 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() - >方法(mev-> id());
qDebug()<< Metacall:<接收器< slot.methodSignature();
}
return Base :: notify(receiver,event);
}
}

int main(int argc,char ** argv){
MetaCallWatcher< QApplication> app(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, 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.

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天全站免登陆