-
Bug
-
Resolution: Invalid
-
P2: Important
-
None
-
5.15.7
-
None
When we have a listmodel and listview is currently not completed yet, but before append an element, we don't get a model count change message.
Somehow this makes sense, as the list is just not ready yet, but when we're listening to count changes or current item changes, the rest of our code is not reflecting the changes either if changes are not emitted.
With the following example, I had three different approaches and three different outcomes:
Item {
Timer {
id: _addTimer
interval: 100
onTriggered: {
console.error("ListViewDebug: append element to model from timer.");
_listview.model.append(_toAdd);
}
}
ListElement { id: _toAdd }
ListModel {
id: theModel
ListElement {}
ListElement {}
// ... several of them
}
ListView {
id: _listview
anchors.fill: parent
anchors.margins: 20
clip: true
model: theModel
delegate: _myDelegate
spacing: 5
Component.onCompleted: {
console.error("ListViewDebug: Listview completed");
}
// we could probably just check for onCountChanged now that I add that example ...
property int modelCount: model.count
property bool first: true
onModelCountChanged: {
console.error("ListViewDebug: Model count changed to " + modelCount);
if (first) {
// SECOND + THIRD APPROACH -- insert element here
// _listview.model.insert(0, _toAdd);
first = false;
}
}
onModelChanged: {
console.error("ListViewDebug: model changed completd" + model.count);
// THIRD APPROACH -- start timer
// _addTimer.restart();
}
onCurrentItemChanged: {
console.error("ListViewDebug: current item changed: " + currentItem);
// FIRST APPROACH -- insert element here
// _listview.model.insert(0, _toAdd);
}
}
Component {
id: _myDelegate
Rectangle {
id: _rect
width: 120
height: 32
color: "blue"
Component.onCompleted: {
_rect.color = "red";
_rect.opacity = Qt.binding(function () { return _listview.currentItem === this ? 1.0 : 0.5 });
}
}
}
}
First case: I insert an element directly on currentItemChanged – outcome: we have a list and the second element is highlighted (=current item) and we see a model count update in the logs.
Second case: I insert the element directly on modelCountChanged. Outcome – the first element stays currentItem and we don't see a model count update.
Third case: I insert the element (as before) on modelCountChanged, but also start a timer that appends an element (this is executed after list view has completed) – outcome: second element is highlighted and we see model count has increased by two, so somehow the count increases, but we don't get the update.