ListView delegate state is lost after model updates
Solved
QML and Qt Quick
-
I have a ListView that has a delegate with a certain
selected
property to visually change some of the items in a certain way. When I setselected = true
on some delegates and the model changes (due to being a QSortFilterProxyModel which is being searched) the delegates all haveselected == false
. Is it normal for them to lose state and can I somehow prevent that? -
@Creaperdown state should not be stored in delegates (ctrl-f "state" in the ListView documentation).
What I would advise you is to use a ItemSelectionModel.
The API is not pleasant to use but it can be useful.An example of how to use it in a Checkbox as a delegate:
readonly property var modelIndex: selectionModel.model.index(model.index, 0) checked: selectionModel.selectedIndexes.includes(modelIndex) onToggled: selectionModel.select(modelIndex, ItemSelectionModel.Toggle)
-