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. Deleting items in a QML listModel
QtWS25 Last Chance

Deleting items in a QML listModel

Scheduled Pinned Locked Moved QML and Qt Quick
10 Posts 7 Posters 22.1k 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.
  • S Offline
    S Offline
    Salem
    wrote on last edited by
    #1

    I'm working on a simple to-do list in QML using listModel, and would like to implement a feature to allow the user to delete multiple items that have been checked off (that have a boolean property 'done').

    Problem is I'm new to QML and can't find any resources that mention how to delete multiple items from a list. If anyone could help, it would be very much appreciated.

    1 Reply Last reply
    1
    • D Offline
      D Offline
      dridk
      wrote on last edited by
      #2

      @import QtQuick 1.1

      Rectangle {
      id:root
      width: 360
      height: 360

      function removeSelection()
      {
          for (var i=0; i<fruitModel.count; ++i)
          {
              if (fruitModel.get(i).selected){
                  fruitModel.remove(i);
                  i=0; //read from the start! Because index has changed after removing
              }
          }
      

      }

      //================= MODEL
      ListModel {
          id: fruitModel
      
          ListElement {
              name: "Apple"
              selected:false
          }
          ListElement {
              name: "Orange"
              selected:false
          }
          ListElement {
              name: "Banana"
              selected:false
          }
      }
      //================ BUTTON : press = remove the selection
      Rectangle {
          id:button
          width: 360
          height: 100
          color: buttonArea.pressed ? "gray" : "lightgray"
          Text {
              anchors.centerIn: parent
              text:"click to delete selection"
          }
          MouseArea{
              id:buttonArea
              anchors.fill: parent
              onPressed: root.removeSelection()
          }
      }
      //================ List
      ListView {
          anchors.top: button.bottom
          anchors.bottom: root.bottom
          width: 360
          clip: true
          model:fruitModel
          //============ Delegate
          delegate: Rectangle {
              width: 360
              height: 50
              border.color: "black"
              color: selected ? "red" :"white"
              Text {
                  anchors.centerIn: parent
                  text: name
              }
              MouseArea {
                  anchors.fill: parent
                  onClicked: fruitModel.setProperty(index,"selected",!selected)
              }
          }
      }
      

      }
      @

      Nothing in Biology Makes Sense Except in the Light of Evolution

      1 Reply Last reply
      1
      • D Offline
        D Offline
        dridk
        wrote on last edited by
        #3

        This is the method if you are using ItemModel.
        If you are a newbe with Qt, use it.
        Otherwise, I suggest you to use a C++ Model. But it's pretty hard if you don't know Qt

        Nothing in Biology Makes Sense Except in the Light of Evolution

        1 Reply Last reply
        0
        • EddyE Offline
          EddyE Offline
          Eddy
          wrote on last edited by
          #4

          AFAIR this tutorial does what you want :
          "Programming with Qt Quick for Symbian and MeeGo Harmattan Devices ":http://qt.nokia.com/learning/guides

          Qt Certified Specialist
          www.edalsolutions.be

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Salem
            wrote on last edited by
            #5

            Thanks, dridk and Eddy!
            Both of your posts have been a big help :)

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              One tip on the code posted by dridk:
              The method is not very efficient. Whenever you iterate over an indexed container while removing items (and thus, changing indices), a nice trick is to iterate backwards. This way, the changing indices do not affect your loop:

              @
              //original
              function removeSelection()
              {
              for (var i=0; i<fruitModel.count; ++i)
              {
              if (fruitModel.get(i).selected){
              fruitModel.remove(i);
              i=0; //read from the start! Because index has changed after removing
              }
              }
              }

              // iterating backwards
              function removeSelection()
              {
              for (var i=fruitModel.count - 1; i >= 0; --i)
              {
              if (fruitModel.get(i).selected){
              fruitModel.remove(i);
              //restarting is no longer needed, and thus we are more efficient :-)
              }
              }
              }
              @

              1 Reply Last reply
              1
              • G Offline
                G Offline
                ganeshgladish
                wrote on last edited by
                #7

                hi everyone.
                i have one doubt ....in this program i am using FolderListModel instead if ListModel.....i cant do this function (fruitmodel.get(i).selected)..please provide some solutions

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  bkerdev
                  wrote on last edited by
                  #8

                  [quote author="Andre" date="1333107326"]One tip on the code posted by dridk:
                  The method is not very efficient. Whenever you iterate over an indexed container while removing items (and thus, changing indices), a nice trick is to iterate backwards. This way, the changing indices do not affect your loop:

                  @
                  //original
                  function removeSelection()
                  {
                  for (var i=0; i<fruitModel.count; ++i)
                  {
                  if (fruitModel.get(i).selected){
                  fruitModel.remove(i);
                  i=0; //read from the start! Because index has changed after removing
                  }
                  }
                  }

                  // iterating backwards
                  function removeSelection()
                  {
                  for (var i=fruitModel.count - 1; i >= 0; --i)
                  {
                  if (fruitModel.get(i).selected){
                  fruitModel.remove(i);
                  //restarting is no longer needed, and thus we are more efficient :-)
                  }
                  }
                  }
                  @
                  [/quote]

                  is good like that.

                  Boris Bker

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    ganeshgladish
                    wrote on last edited by
                    #9

                    thank you for your replay mr bkerdev,

                    please see this link,
                    http://qt-project.org/forums/viewthread/29243/#130702 ....

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      joshmarshall95
                      wrote on last edited by
                      #10

                      I found this very simple solution to removing items, '10' can be changed to model.count to suit apps where the model size is not known.
                      for(var i = 0; i < 10;i++)
                      {
                      gridimgs.remove(0)
                      }

                      1 Reply Last reply
                      -1

                      • Login

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