How to properly notify in QQmlListProperty properties?
Whenever I create Q_PROPERTY
for later use in Qml, I always generate a notification signal to tell qml that the data has changed and needs to be reevaluated.
Now, having a Q_PROPERTY
type QQmlListProperty<T>
, how can I signal that an item has been changed, added, or removed?
Is it possible?
source to share
If you have a list, there can be no signal propertyChanged()
because the stored object reference will remain the same.
There will be no properties inside the list, so no signal will be emitted.
Instead, you can use a descendant QAbstractListModel
that is designed to solve this problem by adding methods for add, insert, etc. in your own methods, which then issues a signal dataChanged
that carries the information you need to look for changes.
Of course, you could implement something similar to yourself by wrapping it QList
in another object that has a signal to inform you about the data change. However, this will not integrate well with QML as a real model, as at least the view will automatically update when a signal is received dataChanged
, and they even only update what is needed.
Not so, if model
from the View
changes directly, as can happen if you manually call modelChanged()
. In this case, it View
will skip information about the changed parts, so it completely recreates itself completely.
source to share