0

I am using Qt 5.9.3. I have following property declared in my app's main.qml

Code:

//main.qml
MyQuickItem {

    property color nextColor
    onNextColorChanged: {
        console.log("The next color will be: " + nextColor.toString())
    }
}

// MyQuickItem.h
class MyQuickItem : public QQuickItem {

}

Question:

How can I make onNextColorChanged be defined in the C++ side?

I know that I can also make nextColor as a property inside C++ class MyQuickItem. like so

// MyQuickItem.h
class MyQuickItem : public QQuickItem {

    Q_PROPERTY(QColor nextColor READ nextColor WRITE setNextColor NOTIFY nextColorChanged)
}

Is it possible to monitor OnNextColorChanged inside MyQuickItem?

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
  • Does this help? https://stackoverflow.com/questions/48813037/qt-how-to-monitor-a-q-property-change-on-c-side-instead-of-qml – Flopp Feb 15 '18 at 17:55
  • 1
    Just connect some slot to `nextColorChanged` signal inside `MyQuickItem`. – Evgeny Feb 15 '18 at 18:00

1 Answers1

3

We can use the QMetaObject to obtain the property and the signal, then we connect it through the old style:

#ifndef MYQUICKITEM_H
#define MYQUICKITEM_H

#include <QQuickItem>
#include <QDebug>

class MyQuickItem : public QQuickItem
{
    Q_OBJECT
public:
    MyQuickItem(QQuickItem *parent = Q_NULLPTR): QQuickItem(parent){}
protected:
    void componentComplete(){
        int index =metaObject()->indexOfProperty("nextColor");
        const QMetaProperty property = metaObject()->property(index);
        if (property.hasNotifySignal()){
            const QMetaMethod s = property.notifySignal();
            QString sig = QString("2%1").arg(QString(s.methodSignature()));
            connect(this, sig.toStdString().c_str() , this, SLOT(onNextColorChanged()));
        }
    }
private slots:
    void onNextColorChanged(){
        int index =metaObject()->indexOfProperty("nextColor");
        const QMetaProperty property = metaObject()->property(index);
        qDebug()<<"color" << property.read(this);
    }
};

#endif // MYQUICKITEM_H

The complete example can be found in the following link.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241