Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-126796

QML ListView insert Item to index(0), the section layout error

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P2: Important P2: Important
    • None
    • 5.15.2, 6.7.2
    • None
    • Windows

      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()
                  }
              }
          }
      }
      

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            gongjianbo1992 建波 龚
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:

                There are no open Gerrit changes