How to remove element from ListView by clicking on this element's remove button
Solved
QML and Qt Quick
-
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
-
@Shrinidhi-Upadhyaya Hello ,i want to remove some checked listview elements...for that can you help pls..
Like i created a listview and gave checkbox so i want to remove the items which are checked by clicking one delete button ,some are saying i can use signal slots in cpp and connect to the qml but i really have no idea how to do that,can you please help me with that.