Qmetaobject.h文件整体分析

Qmetaobject.h文件整体分析

又是忙碌的一天,感觉时间过的太快了,一天天的时间就这样匆匆流走,想想自己的所得,似乎怎么都赶不上时间流逝的飞快。只能尽量尝试压榨自己的时间,看能不能得到更多的收获,好对得起这人生最美好的年华。

好了,言归正传,我们今天再次深入Connect函数,不知道今天又会有哪些收获呢?

昨天看到了,最开始的部分,是对信号和槽名称的检测,接下来时要做一次检索了,来获得信号的索引号:

if (!check_signal_macro(sender, signal, "connect", "bind"))

        return false;

    const QMetaObject *smeta = sender->metaObject();

    const char *signal_arg = signal;

    ++signal; //skip code

    int signal_index = QMetaObjectPrivate::indexOfSignalRelative(&smeta, signal);

跟踪索引函数:indexOfSignalRelative,得到了如下的代码:

int i = -1;

    while (*baseObject) {

        const QMetaObject *const m = *baseObject;

        for (i = priv(m->d.data)->methodCount-1; i >= 0; --i)

            if ((m->d.data[priv(m->d.data)->methodData + 5*i + 4] & MethodTypeMask) == MethodSignal

                && strcmp(signal, m->d.stringdata

                + m->d.data[priv(m->d.data)->methodData + 5*i]) == 0) {

                break;

            }

        if (i >= 0)

            break;

        *baseObject = m->d.superdata;

    }

看到这里,感觉这样的分析不怎么样,不是很明白里面的机制了,看样子要仔细的看下元对象系统的整体实现了,需要从面向对象的角度去分析了。

元对象系统主要是在corelib/kernel/目录下的qmetaobject.hqmetaobject.cppqobjcetdefs.h 中,在qobjcetdefs.h中,主要定义了QMetaObjectQMetaObjectExtraData两个结构体,C++中已经没有了 C中结构体的概念,struct修饰符修饰的是一个特殊的类,默认为 qmetaobject.h中则是整个实现部分的定义了。

qmetaobject.h中定义了这些类:QMetaMethodQMetaEnumQMetaPropertyQMetaClassInfo

qmetaobject_p.h中定义了:QMetaObjectPrivate

接下来对这些类做一点分析:QMetaMethod类中保存了元对象系统的方法,其中最主要的就是invoke方法,该部分在Qt中有具体的文档说明:

Invokes this method on the object object. Returns true if the member could be invoked. Returns false if there is no such member or the parameters did not match.

The invocation can be either synchronous or asynchronous, depending on the connectionType:

  • If connectionType is Qt::DirectConnection, the member will be invoked immediately.
  • If connectionType is Qt::QueuedConnection, a QEvent will be posted and the member is invoked as soon as the application enters the main event loop.
  • If connectionType is Qt::AutoConnection, the member is invoked synchronously if object lives in the same thread as the caller; otherwise it will invoke the member asynchronously.

The return value of this method call is placed in returnValue. If the invocation is asynchronous, the return value cannot be evaluated. You can pass up to ten arguments (val0, val1, val2, val3, val4, val5, val6, val7, val8, and val9) to this method call.

这里有调用的例子可以参考:

int methodIndex = pushButton->metaObject()->indexOfMethod("animateClick()");

 QMetaMethod method = metaObject->method(methodIndex);

 method.invoke(pushButton, Qt::QueuedConnection);

可以通过QGenericArgument进行传参。

时间不早了,今天只能研究到这里了,要睡觉了,明天还要早起。

 

20091021日星期三 2350

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值