我有一堂课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; };
和班级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; };
我想做一个这样的信号/插槽连接
QObject::connect(&objA, SIGNAL(aChanged(int)), &objB, SLOT(setB(int)));
只知道名称的属性。
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;
但是
metaPropertyA.notifySignal().methodSignature()
不返回函数的指针,我也不知道如何获取 set 方法指针。