0

I would like to catch QEvent in my custom cpp QObject (MyObject) if some of the properties is changed (QEvent::DynamicPropertyChange):

class MyObject : public QObject
{
    Q_OBJECT
    Q_PROPERTY(bool value MEMBER mValue)

public:
    MyObject();

    bool event(QEvent *e) override
    {
        qDebug() << "EVENT RECIEVED" << e->type();
        return QObject::event(e);
    }

private:
    bool mValue = false;
};

It works great if i do this in cpp:

MyObject obj = MyObject();
obj.setProperty("val", true);

But it does not if I try to change property in QML:

MyObject {
    id: obj
}

Button{
    id: button
    text: "set value"
    onClicked: function () {
        console.log('button clicked');
        obj.value = true;
    }
}

This is a simple github example of this.

Any ideas ?

  • I think the docs are clear https://doc.qt.io/qt-5/qdynamicpropertychangeevent.html: *Dynamic property change events are sent to objects when properties are dynamically added, changed or removed **using QObject::setProperty()***. And QML doesn't use that method to update properties – eyllanesc Dec 20 '21 at 21:50
  • The dynamic properties(like `val`) are not the same as the Q_PROPERTY(like `value`), therefore the event is not triggered. – eyllanesc Dec 20 '21 at 21:52
  • Ye, but i assumed that QML use the same approach... – Kapustin Alexander Dec 20 '21 at 21:53
  • Well, what you assume is wrong. If you give yourself when the dynamic properties do not have any associated signal, that is why an event is used, for example to update the qss. But QML uses other methods, if you want to hear when "value" changes then create the getter, setter and a token (don't use member). And in the setter invoke the slot (or connect a slot to the signal) – eyllanesc Dec 20 '21 at 21:59
  • I have a big number of such properties. And i want to pass property name and new value in some singal if something changed.. Yes, i can use getter-setter mechanism for this. I think cpp macros magic can help me with this issue. Thank you! – Kapustin Alexander Dec 20 '21 at 22:15

0 Answers0