Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to remove element from ListView by clicking on this element's remove button



  • Hi all, like in the tittle - I dynamically add some elements to the ListView, those elements contain button that should remove from list this exact element. For now I cannot dig into element's index in ListModel to remove it. Can anyone help?

    import QtQuick 2.9
    import QtQuick.Window 2.2
    import QtQuick.Controls 1.4
    import QtQuick.Layouts 1.2
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
    
        ColumnLayout {
            id: layout_column
            anchors.top: parent.top
            anchors.left: parent.left
            anchors.right: parent.right
    
            Rectangle {
                id: rect
                width: 180; height: 200
    
                Component {
                    id: contactDelegate
                    Item {
                        width: 180; height: 40
                        Row {
                            Text { text: '<b>Name:</b> ' + name }
                            Text { text: '<b>Number:</b> ' + number }
                            Button{
                                     text: "removeMe"
                                     onClicked:
                                     { //HERE I WANT TO REMOVE IT}
                            }
    
                        }
                    }
                }
                ListView {
                    id: listView
                    property ListModel listModel: ListModel {}
                    anchors.fill: parent
                    model: listModel
                    delegate: contactDelegate
                }
            }
    
            Button{
                     id: addButton; height: 50; width: 50; text: "Add1"
                     onClicked:
                     {
                         listView.listModel.insert(0, { "name": "name", "number": 50})
                     }
    
            }
    }
    


  • Hi @Kyeiv,

      Button{
              text: "removeMe" + index
              onClicked:
                        { //HERE I WANT TO REMOVE IT}
                          listView.listModel.remove(index)
                        }
        }


  • @Shrinidhi-Upadhyaya thank you very much! it solved the issue