我想编写一个属性连接器,用于以QML方式连接C ++ Qt中的属性。它可能是这样的:
new PropConnector ( receiver, "propName", sender, "propName" );
propConnector类是一个小型QObject类,它使用sender作为父级,侦听属性更改信号并设置接收器的相应属性。像这样:
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 ) );
}
};
唯一的问题是,如何找到我想听的属性名称的正确信号名称?我想,这是可能的,但直到现在我还是不会通过阅读元对象文档找到它。
编辑:根据凯文的建议,我能够实施该课程。
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() ) );
}
答案 0 :(得分:2)
您可以从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
,以类似的方式获取它。