dataChanged not updating a qml listview

I was trying to optimize some code in Kalendar and replace a beginResetModel/endResetModel with a dataChanged since only the data were updated/reset, but the size of the model didn’t change.

This was my code:

Q_EMIT dataChanged(index(0, 0), index(rowCount({}), 0));`

Unfortunately, this didn’t work and after a dataChanged my data method didn’t get called. Can you spot the mistake? It turned out to by an off my one mistake. index(rowCount({}), 0) is outside of the model and has such an invalid index. This made the entire dataChanged silently fail.

Here is the working code

Q_EMIT dataChanged(index(0, 0), index(rowCount({}) - 1, 0));`
4 Likes

Been there, done that in Flatpak KCM which conditionally hides its second half (advances sections) so that number of entries in model does not always correspond to the number of rows in a ListView. Indeed, QtQuick views are rather picky about incorrect indices in those signals.

I’m relatively sure that by hooking a QAbstractItemModelTester to your model it would have warned you that you where doing it wrong, so that’s something you can quickly try next time you have model problems)

2 Likes