Deleting items in a QML listModel



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



  • @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)
            }
        }
    }
    

    }
    @



  • 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


  • Moderators

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



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



  • 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 :-)
    }
    }
    }
    @



  • 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



  • [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.



  • thank you for your replay mr bkerdev,

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



  • 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)
    }


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.