I've created c++ class with QML attached properties to use it in qml.
Now I want to add attached property to one QQuickItem object and propagate this attached property for all QQuickItem child items (those which returned by QQuickItem::childItems() not by QObject::children()). I can easily iterate over existing QQuickItem child items and add attached properties to them, however I can't find out how to listen for added/removed child items for certain QQuickItem. No signals, no events only overriding QQuickItem::itemChange() method.
How can I listen for adding/removing QQuickItem item children?
I've found only QQuickItemChangeListener but this class is from private API.
UPD1: Implementation of attached property:
class MyProp : public QObject {
Q_OBJECT
Q_PROPERTY(int prop ...)
public:
explicit MyProp(QObject * object = nullptr) : QObject(object) {
auto item = qobject_cast<QQuickItem *>(object);
if(item) {
for(auto child : _item->childItems()) {
QObject * attached = qmlAttachedPropertiesObject<MyProp>(child, true);
... // needs to listen here for adding/removing child items
}
}
}
static MyProp * qmlAttachedProperties(QObject * object) {
return new MyProp(object);
}
}
QML_DECLARE_TYPEINFO(MyProp, QML_HAS_ATTACHED_PROPERTIES)
QQuickAttachedObjectclass. - Kamil Zaripov