Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to remove element from ListView by clicking on this element's remove button

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
listviewlistmodelcomponent
4 Posts 3 Posters 5.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kyeiv
    wrote on last edited by Kyeiv
    #1

    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})
                     }
    
            }
    }
    
    1 Reply Last reply
    0
    • Shrinidhi UpadhyayaS Offline
      Shrinidhi UpadhyayaS Offline
      Shrinidhi Upadhyaya
      wrote on last edited by
      #2

      Hi @Kyeiv,

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

      Shrinidhi Upadhyaya.
      Upvote the answer(s) that helped you to solve the issue.

      K P 2 Replies Last reply
      3
      • Shrinidhi UpadhyayaS Shrinidhi Upadhyaya

        Hi @Kyeiv,

          Button{
                  text: "removeMe" + index
                  onClicked:
                            { //HERE I WANT TO REMOVE IT}
                              listView.listModel.remove(index)
                            }
            }
        
        K Offline
        K Offline
        Kyeiv
        wrote on last edited by
        #3

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

        1 Reply Last reply
        1
        • Shrinidhi UpadhyayaS Shrinidhi Upadhyaya

          Hi @Kyeiv,

            Button{
                    text: "removeMe" + index
                    onClicked:
                              { //HERE I WANT TO REMOVE IT}
                                listView.listModel.remove(index)
                              }
              }
          
          P Offline
          P Offline
          Priti
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved