I want to write a property connector for connecting properties in C++ Qt in QML manner. It could be like this:
new PropConnector ( receiver, "propName", sender, "propName" );
The propConnector class is a small QObject class which uses sender as parent, listens on property-changed signal and sets the appropriate property of the receiver. Like this:
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 ) );
}
};
The only problem is, how can I discover the proper signal name for the property name I want to listen to? I guess, it is possible, but I gouldn't find out till now by reading the meta-object documentation.
Edit: With the suggestion of Kevin I was able to implement the 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() ) );
}
You can get that information from the 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);
}
}
Untested, might not compile. metaMethodOfSlot
would be the QMetaMethod
for the receiver slot, obtain that in a similar way.