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. Delete checked items from a QML List view
QtWS25 Last Chance

Delete checked items from a QML List view

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 1.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.
  • P Offline
    P Offline
    PouryaTorabi
    wrote on last edited by
    #1

    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.

    ODБOïO 1 Reply Last reply
    0
    • P PouryaTorabi

      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.

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by ODБOï
      #2

      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

      P 1 Reply Last reply
      2
      • ODБOïO ODБOï

        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

        P Offline
        P Offline
        PouryaTorabi
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • P Offline
          P Offline
          PouryaTorabi
          wrote on last edited by
          #4

          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 )
             }
          }
          
          ODБOïO 1 Reply Last reply
          0
          • P PouryaTorabi

            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 )
               }
            }
            
            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by
            #5

            @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)
            
            1 Reply Last reply
            1
            • P Offline
              P Offline
              Priti
              wrote on last edited by
              #6

              Can you please share the backend c code that does the deletion part

              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