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

Removing item from within delegate causes item properties to disappear

    XMLWordPrintable

Details

    Description

      When you call listModel.remove(index) from within the delegate of a Repeater, and then try to access properties of either the item OR the surrounding context, QML throws a ReferenceError: not found.

      This code worked in Qt 4.8.4 and I discovered it while porting.

      The example below reproduces the problem.

      import QtQuick 2.2
      import QtQuick.Window 2.1
      
      Window {
          id: root
          visible: true
          width: 360
          height: 360
          signal clicked(int index)
      
          onClicked: {
              console.log("clicked", index)
          }
      
          ListModel {
              id: myList
              ListElement { x: 1; y: 2 }
              ListElement { x: 1; y: 2 }
              ListElement { x: 1; y: 2 }
              ListElement { x: 1; y: 2 }
          }
      
          Repeater {
              anchors {
                  left: parent.left
                  top: parent.top
                  margins: 20
              }
      
              model: myList
              delegate: myDelegate
          }
      
          Component {
              id: myDelegate
              Rectangle {
                  width: 100
                  height: 100
                  color: "green"
                  MouseArea {
                      anchors.fill: parent
                      onClicked: {
                          myList.remove(index)
                          root.clicked(index)
                      }
                  }
              }
          }
      
      }
      

      EXPECTED RESULT:
      console displays
      clicked -1
      clicked -1
      clicked -1
      clicked -1

      ACTUAL RESULT
      console displays
      qrc:/main.qml:44: ReferenceError: root is not defined
      qrc:/main.qml:44: ReferenceError: root is not defined
      qrc:/main.qml:44: ReferenceError: root is not defined
      qrc:/main.qml:44: ReferenceError: root is not defined

      Attachments

        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
            mpnolan Mike Nolan
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes