How to correctly notify in QQmlListProperty properties?

1k Views Asked by At

Whenever I was creating a Q_PROPERTY for later use in Qml I always created a notify signal to tell qml that data changed and needs to be reevaluated.

Now having a Q_PROPERTY of the type QQmlListProperty<T> how can I signalize that an item has been modified, added or removed?

Is this even possible?

1

There are 1 best solutions below

6
On

If you have a list there can't be a propertyChanged() signal, because the object reference stored will remain the same.
Within the list there won't be properties, so no signal is emitted.

You could instead use a descendent of QAbstractListModel which is designed to handle this problem, by wrapping the methods to append, insert etc. in own methods, that then will emit a dataChanged signal that carries the information necessary to find the changes.

Of course you could implement something similar yourself by wrapping a QList in another object, that has a signal that will inform you of the data change. However this won't integrate that nicely with QML as a real model, for at least view will update automatically, when the dataChanged signal is received, and they even only update what is necessary.

Not so, if the model of the View is changed directly, as might happen, if you manually call modelChanged(). In this case, the View would miss the information about the changed parts, so it will just recreate itself completely.