Qt Metadata

MarsCactus發表於2024-09-17

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 : " << 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

methodoffset : 16
methodCount for Child : 22
signal index : 17

相關文章