从元对象中获取 属性 个更改的通知程序

Obtain property changed notifier from meta-object

我想编写一个 属性 连接器,用于以 QML 方式在 C++ Qt 中连接属性。可能是这样的:

new PropConnector ( receiver, "propName", sender, "propName" );

propConnector class 是一个小的 QObject class,它使用发送者作为父对象,监听 属性-changed 信号并设置适当的 属性 接收者。像这样:

class PropConnector : public QObject
{
    Q_OBJECT
private:
    QObject *_sender;
    QObject *_receiver;
    QString _senderProp;
    QString _receiverProp;
public:
    PropConnector(QObject *sender, QObject *receiver, QString senderProp, QString receiverProp) : 
      QObject(sender)
      ,_sender(sender)
      ,_receiver(receiver)
      ,_senderProp(senderProp)
      ,_receiverProp(receiverProp)
    { 
        // Is it possible to get the proper property-changed notifier signal?
        //connect(sender, SIGNAL( on<PROPERTY>Changed() ), this, SLOT( forward ) );
    }

private slots:
    void forward()
    {
        _receiver->setProperty( receiverProp, _sender->property( senderProp ) );
    }
};

唯一的问题是,如何为我想收听的 属性 名称找到正确的信号名称?我想,这是可能的,但我直到现在才通过阅读元对象文档找到答案。

编辑: 在 Kevin 的建议下,我得以实施 class。

propconnector.h

#ifndef PROPCONNECTOR_H
#define PROPCONNECTOR_H

#include <QObject>
#include <QString>

class PropConnector : public QObject
{
    Q_OBJECT
private:
    QObject *_sender;
    QObject *_receiver;
    QString _senderProperty;
    QString _receiverProperty;
public:
    PropConnector(QObject *sender, QString senderProperty, QObject *receiver, QString receiverProperty);

private slots:
    void forward();
};

#endif // PROPCONNECTOR_H

propconnector.cpp

#include "propconnector.h"
#include <QMetaObject>
#include <QMetaProperty>

PropConnector::PropConnector(QObject *sender, QString senderProperty, QObject *receiver, QString receiverProperty) : 
  QObject(sender)
  ,_sender(sender)
  ,_receiver(receiver)
  ,_senderProperty(senderProperty)
  ,_receiverProperty(receiverProperty)
{ 
    const QMetaObject *senderMeta = sender->metaObject();
    const int index = senderMeta->indexOfProperty(senderProperty.toUtf8().constData());
    if (index != -1) {
        const QMetaProperty p = senderMeta->property(index);
        if ( p.hasNotifySignal() ) {
            const QMetaMethod s = p.notifySignal();

            QString sig = QString("2%1").arg(s.signature());
            const char *ssig = SLOT(forward());
            bool ok = connect(sender, sig.toStdString().c_str() , this, SLOT(forward()));   
            int i=0;
            i++;
        }
    }       
}

void PropConnector::forward()
{
    _receiver->setProperty( _receiverProperty.toStdString().c_str(), _sender->property( _senderProperty.toStdString().c_str() ) );
}

您可以从 QMetaProperty

中获取该信息
QMetaObject *senderMeta = sender->metaObject();
const int index = senderMeta->indexOfProperty(senderProperty.toUtf8().constData());
if (index != -1) {
    const QMetaProperty property = senderMeta->property(index);
    if (property.hasNotifySignal) {
        const QMetaMethod notifySignal = property.notifySignal();

        connect(sender, notifySignal, receiver, metaMethodOfSlot);     
    }
}

未经测试,可能无法编译。 metaMethodOfSlot 将是接收器插槽的 QMetaMethod,以类似的方式获得。