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: 360function 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) } } }
}
@ -
AFAIR this tutorial does what you want :
"Programming with Qt Quick for Symbian and MeeGo Harmattan Devices ":http://qt.nokia.com/learning/guides -
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)
}