artoon
artoon

Reputation: 739

Introspection with QMetaProperty

I have a class ObjectA

class ObjectA : public QObject
{
    Q_OBJECT
    Q_PROPERTY(int _a READ a WRITE setA NOTIFY aChanged)
public:
    ObjectA(int a) : _a(a) {}
    int a() const { return _a;}
public slots:
    void setA(int a) { _a = a; emit aChanged(_a);}
signals:
    void aChanged(int);
private:
    int _a;
};

and the class ObjectB

class ObjectB : public QObject
{
    Q_OBJECT
    Q_PROPERTY(int _b READ b WRITE setB NOTIFY bChanged)
public:
    ObjectB(int b) : _b(b) {}
    int b() const { return _b;}
public slots:
    void setB(int b) { _b = b; emit bChanged(_b);}
signals:
    void bChanged(int);
private:
    int _b;
};

And I would like do a signal/slot connection like this

QObject::connect(&objA, SIGNAL(aChanged(int)), &objB, SLOT(setB(int)));

knowing only name's properties.

ObjectA objA(10);
ObjectB objB(5);
QObject * objectA = &objA;
const QMetaObject* metaObjectA = objectA->metaObject();
QMetaProperty metaPropertyA = metaObjectA->property(metaObjectA->indexOfProperty("_a"));


QObject * object = &objB;
const QMetaObject* metaObjectB = object->metaObject();
QMetaProperty metaPropertyB = metaObjectB->property(metaObjectB->indexOfProperty("_b"));

QObject::connect(&objA, metaPropertyA.notifySignal().methodSignature(), &objB, ... );

objA.setA(2);
std::cout << "objA.a() = " << objA.a() << " objB.b() = " << objB.b() << std::endl;

but the

metaPropertyA.notifySignal().methodSignature()

doesn't return the function's pointer and I don't how get the set method pointer.

Upvotes: 2

Views: 852

Answers (1)

j123b567
j123b567

Reputation: 3439

metaPropertyA.notifySignal().signature() does not return method pointer but its signature (text representation) - in your case "aChanged(int)" and this is what you need for connect().

If you rather want to call this directly, you can use metaPropertyA.notifySignal().invoke(...).

EDIT: you need to connect some number and method name as SIGNAL and SLOT macro do

so you need to programaticcaly create

QObject::connect(&objA, "2aChanged(int)", &objB, "1setB(int)");

but how to get "1setB(int)" is another task

EDIT2: I don't know what the number mean, because it is not index

Upvotes: 1

Related Questions