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

QML Views: Too many delegates instantiated unnecessarily with (begin|end)InsertRows()

    XMLWordPrintable

Details

    Description

      Code

      ApplicationWindow {
          id: root
          width: 640
          height: 480
          visible: true
      
          header: Row {
              Button {
                  text: "Clear"
                  onClicked: model.resetData(0) // Calls (begin|end)ResetModel()
              }
              Button {
                  text: "Reset Data"
                  onClicked: model.resetData(100) // Calls (begin|end)ResetModel()
              }
              Button {
                  text: "Prepend Data"
                  onClicked: model.prependData(100) // Calls (begin|end)InsertRows()
              }
          }
      
          MyModel {
              id: model
          }
      
          ListView {
              id: view
              width: 400
              height: 400
              model: model
      
              onCountChanged: positionViewAtEnd()
      
              delegate: Rectangle {
                  id: delegate
                  required property int index
                  required property string display
      
                  width: view.width
                  height: view.height / 10
                  color: (index % 2) ? "beige" : "brown"
      
                  Text {
                      anchors.centerIn: parent
                      font.pixelSize: 15
                      color: (delegate.index % 2) ? "black" : "white"
                      text: delegate.display
                  }
                  Component.onCompleted: console.log("New:", display)
                  Component.onDestruction: console.log("Destroyed:", display)
              }
          }
      }
      

       

      Steps to Reproduce

      1. Build and run the attached project
      2. Click "Reset Data" a few times
      3. Click "Clear"
      4. Click "Prepend Data"

       

      Outcomes

      • At step #2, 21 delegates are created each click (reasonably expected)
      • At step #4, 117 delegates are created on the first click (very unexpected)

       

      Notes

      • This example demonstrates the example using ListView, but it also affects other views (e.g. GridView)

      Attachments

        Issue Links

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

          Activity

            People

              qt.team.quick.subscriptions Qt Quick and Widgets Team
              skoh-qt Sze Howe Koh
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes