Details
-
Bug
-
Resolution: Invalid
-
P3: Somewhat important
-
None
-
5.15, 6.1.0 Beta2
Description
Initial setting of ListView currentIndex is ignored with dynamic model generation.
Please see attached test qml file: listview_test4.qml.
In this test code ListView.model was dynamically generated before ListView.onCompleted.
But default setting of currentIndex (at line 49) does not work.
With this test code, it seems that QQuickItemViewChangeSet::applyChanges() in qquickitemview.cpp misbehaves.
void QQuickItemViewChangeSet::applyChanges(const QQmlChangeSet &changeSet) { pendingChanges.apply(changeSet); int moveId = -1; int moveOffset = 0; for (const QQmlChangeSet::Change &r : changeSet.removes()) { itemCount -= r.count; if (moveId == -1 && newCurrentIndex >= r.index + r.count) { newCurrentIndex -= r.count; currentChanged = true; } else if (moveId == -1 && newCurrentIndex >= r.index && newCurrentIndex < r.index + r.count) { // current item has been removed. if (r.isMove()) { moveId = r.moveId; moveOffset = newCurrentIndex - r.index; } else { currentRemoved = true; newCurrentIndex = -1; if (itemCount) newCurrentIndex = qMin(r.index, itemCount - 1); } currentChanged = true; } } for (const QQmlChangeSet::Change &i : changeSet.inserts()) { if (moveId == -1) { if (itemCount && newCurrentIndex >= i.index) { newCurrentIndex += i.count; currentChanged = true; } else if (newCurrentIndex < 0) { newCurrentIndex = 0; currentChanged = true; } else if (newCurrentIndex == 0 && !itemCount) { // this is the first item, set the initial current index currentChanged = true; } } else if (moveId == i.moveId) { newCurrentIndex = i.index + moveOffset; } itemCount += i.count; } }
The initial value of newCurrentIndex of above function is 1 (it's from initial setting of currentIndex: 1 at line 49 in listview_test4.qml) and this function was called 5 times (the number of model items) before LIstView.onCompleted.
This results in wrong currentIndex of ListView.
The only workaround is setting initial currentIndex at ListView.onComplted. (see the line 50 in listview_test4.qml)
Attachments
Issue Links
- relates to
-
QTBUG-38510 ListView currentIndex incorrect if changed from onRowsInserted in the model
- Reported
-
QTBUG-39146 Setting ListView currentIndex doesn't set initial item correctly in certain circumstances
- Closed