-
Bug
-
Resolution: Unresolved
-
P2: Important
-
None
-
5.15.2, 6.7.2
-
None
Whether it is a QML model or a Cpp model, when inserting new Item into index(0), if the first section is not visible (the scroll bar scrolls down a section), it will cause layout errors (a new section appears). If index>0, that is OK.
import QtQuick 2.15 import QtQuick.Window 2.15 import QtQuick.Controls 2.15 Window { width: 640 height: 480 visible: true title: qsTr("ListView Section") ListView { id: list_view anchors.fill: parent anchors.margins: 10 highlightFollowsCurrentItem: true highlightMoveDuration: 0 ScrollBar.vertical: ScrollBar { policy: ScrollBar.AlwaysOn } model: ListModel { id: list_model } section{ property: "myGroup" criteria: ViewSection.FullString labelPositioning: ViewSection.InlineLabels delegate: Rectangle { width: 300 height: 40 color: "black" Text { anchors.centerIn: parent text: "Group " + section color: "white" } } } delegate: Rectangle { width: 300 height: 40 border.color: "black" Text { anchors.centerIn: parent text: String("%1: %2").arg(model.index).arg(model.msg) } } } Row { anchors.bottom: parent.bottom spacing: 10 Button { text: "Step 0: init model" onClicked: { for (let i = 0; i < 3; i++) { list_model.append({myGroup:"A", msg:"hi"}) } for (let i = 0; i < 20; i++) { list_model.append({myGroup:"B", msg:"hi"}) } } } Button { text: "Step 1: scroll to bottom && insert 0" onClicked: { list_model.insert(0, {myGroup:"A", msg:"2"}) list_view.currentIndex = 0 // have no effect // list_view.forceLayout() } } } }