Qt Metadata

1.code

class GranPa: public QObject
{
    Q_OBJECT
public:
    explicit GranPa(QObject *parent = nullptr);

signals:
    void gran_siga();
    void gran_sigb();
    void gran_sigc();
public slots:
    void gran_slota();
    void gran_slotb();
    void gran_slotc();
};

GranPa::GranPa(QObject *parent)
    : QObject{parent}
{}

void GranPa::gran_slota()
{
    qDebug() << __FUNCTION__;
}

void GranPa::gran_slotb()
{

}

void GranPa::gran_slotc()
{

}

class Parent : public GranPa
{
    Q_OBJECT
public:
    explicit Parent(QObject *parent = nullptr);

signals:
    void parent_siga();
    void parent_sigb();
    void parent_sigc();
public slots:
    void parent_slota();
    void parent_slotb();
    void parent_slotc();
};

Parent::Parent(QObject *parent)
    : GranPa{parent}
{}

void Parent::parent_slota()
{
    qDebug() << __FUNCTION__;
}

void Parent::parent_slotb()
{

}

void Parent::parent_slotc()
{

}

class Add
{
public:
    int a;
    int b;
    Add(){}
    Add(const Add& add) {
        this->a = add.a;
        this->b = add.b;
    }
        virtual void say()
        {}
    
} ;


typedef struct
{
    int a;
    int b;
} Sub;
class Child : public Parent
{
    Q_OBJECT
    Q_PROPERTY(Add propertyA READ getPropertyA WRITE setPropertyA)
    Q_PROPERTY(Sub propertySub READ getPropertySub WRITE setPropertySub)
public:
    explicit Child(QObject *parent = nullptr);
    Add getPropertyA()
    {
        return _add;
    }
    Sub getPropertySub()
    {
        return _sub;
    }
    void setPropertyA(Add param)
    {
        _add = param;
    }

    void setPropertySub(Sub param)
    {
        _sub = param;
    }
signals:
    void child_siga(int a, int b);
    void child_sigb(Add param);
    void child_sigc();
public slots:
    int child_slota(int a, int b);
    int child_slotb(Add param);
    void child_slotc();
private:
    Add _add;
    Sub _sub;
};

Child::Child(QObject *parent)
    : Parent{parent}
{}

int Child::child_slota(int a, int b)
{
    qDebug()<< __FUNCTION__ << " a + b = " << a+b;
    return a + b;
}

int Child::child_slotb(Add param)
{
    qDebug() << __FUNCTION__ << " a + b = " << param.a + param.b ;
    return param.a + param.b;
}

void Child::child_slotc()
{

}

int main(int argc, char* argv[])
{
    QCoreApplication a(argc, argv);

    Child child;

    qRegisterMetaType<Add>("Add");
 
    for (const QMetaObject* mo = child.metaObject(); mo != nullptr; mo = mo->superClass())
    {
        cout << mo->className() <<" methodoffset : " << mo->methodOffset() << endl;
        cout << mo->className() <<" methodCount  : " << mo->methodCount() << endl;
        cout << mo->className() << " propertyOffset  : " << mo->propertyOffset() << endl;
        cout << mo->className() << " propertyCount  : " << mo->propertyCount() << endl;
    
        cout << "=================================================================" << endl;
    }


    const QMetaObject* mo = child.metaObject();
    cout << "methodoffset : " << mo->methodOffset() << endl;
    cout << "methodCount for Child : " << mo->methodCount() << endl;
    const char* signal = "child_sigb(Add)";
    cout << "signal index for child_sigb(Add): " << mo->indexOfSignal(signal) << endl;

    return a.exec();
}
==========================Console Output======================

Child methodoffset : 16
Child methodCount  : 22
Child propertyOffset  : 1
Child propertyCount  : 3
=================================================================
Parent methodoffset : 10
Parent methodCount  : 16
Parent propertyOffset  : 1
Parent propertyCount  : 1
=================================================================
GranPa methodoffset : 4
GranPa methodCount  : 10
GranPa propertyOffset  : 1
GranPa propertyCount  : 1
=================================================================
QObject methodoffset : 0
QObject methodCount  : 4
QObject propertyOffset  : 0
QObject propertyCount  : 1
=================================================================

signal index for child_sigb(Add): 17
slot index for child_slotb(Add): 20
method index for child_sigb(Add): 17
method index for child_slotb(Add): 20

结论:

  1. QMetaObject中的signal 和 slot 都是 method,每个method 都有一个整型索引。
  2. 针对一个signal其索引可以分为 绝对索引和相对索引, 绝对索引是从顶层父类对应的QMetaObject中的第一个signal 的 索引 0 开始计算。 相对索引是从本类对应的QMetaObject中的第一个signal索引 0 开始计算。例如:
    针对Child类的child_sigb signal其绝对索引为:10 (Parent 含3个signal,Granpa 含有 3个signal,QObject 含有 2个signal,而child_sigb 在Child中是第二个signal) , 相对索引为:1
  3. Qt中信号 槽 机制 创建链接 内部数据结构中是以 signal的绝对索引 和 槽的相对索引 作为记录的。例如:
    QObject::connect(&child, SIGNAL(child_sigb(Add)), &child, SLOT(child_slotb(Add)), Qt::QueuedConnection);

moc 生成的child_moc.cpp====================









Queued Connection==================




posted @ 2024-09-17 13:16  MarsCactus  阅读(9)  评论(0编辑  收藏  举报