2017-01-08 53 views
0

我想寫一個屬性連接器,用於以QML方式連接C++ Qt中的屬性。這可能是這樣的:從元對象中獲取屬性改變的通知者

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

的propConnector類是使用發送者作爲父小QObject的類,偵聽屬性變更後的信號,並且將接收器的合適的屬性。像這樣:

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())); 
} 

回答

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,以類似的方式獲得。

+0

很酷,這有幫助。接收器信號並不重要,因爲連接只有一個方向。信號從連接器類自身發出。 我已經在問題編輯中發佈了代碼。 –