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

Delete checked items from a QML List view



  • Hello every one
    I have a simple list view that list my current files in a directory, each item has a check box before it in the list.
    0_1550324814127_1.png
    I want to check the files I want and click on the delete button to delete them. I have a c++ backend that does the deletion part, I just don't know how to loop through the list and check the status of the check boxes, here is my list view code:

    ListView {
                    width: 150
                    height: 120
                    id: jobListView
                    clip: true
                    model: jobListModel
                    delegate: jobListDelegate
    
                    ScrollBar.vertical: ScrollBar {
                        policy: ScrollBar.AlwaysOn
                    }
                }
    

    and here is my list model:

    ListModel {
                id: jobListModel
                ListElement {
                    name: "No one"
                }
                ListElement {
                    name: "Any one"
                }
            }
    

    and here is my delegate:

    Component {
                id: jobListDelegate
                Item {
                    width: 180; height: 40
                    Layout.fillHeight: true
                    Layout.fillWidth: true
                    RowLayout{
                        Layout.fillHeight: true
                        Layout.fillWidth: true
                        CheckBox{
                        }
    
                        Text {
                            text: name
                        }
                    }
                }
            }
    

    just help me loop through the list view and check if the check box is checked.



  • hi @PouryaTorabi
    simply add a bool for the checkBox in your delegate component

    property bool boxChecked : checkBoxId.checked
    

    then the listview has index property so do

    for (var i in jobListModel.model){
              console.log(jobListModel.model[i].boxChecked )
          }
    

    or you can do it with alias instead of bool



  • @LeLev First of all you mentioned list view but in your loop you wrote jobListModel, I think I should use jobListView right? Then, you said to declare a property in delegate, how is it related to the model?
    I have written your code and it says: qml:undefined.



  • Actually you put me in the right way, thank you, and this is the for loop that works for me:

    for (var i = 0, l = jobListView.contentItem.children.length-1; i < l; i++) {
    if (jobListView.contentItem.children[i].boxChecked )
       {
           console.log(jobListView.model.get(i).name )
       }
    }
    


  • @PouryaTorabi hi
    you can change

    for (var i = 0, l = jobListView.contentItem.children.length-1; i < l; i++)
    

    to

    for(var i in jobListView.contentItem.children)
    

Log in to reply