I've successfully added elements using
list.push(element)
But how do I remove them? I've tried the following, but none of them seem to work.
list.pop()
list.pop_front()
list.remove()
list.remove(int)
list.remove(element)
I've successfully added elements using
list.push(element)
But how do I remove them? I've tried the following, but none of them seem to work.
list.pop()
list.pop_front()
list.remove()
list.remove(int)
list.remove(element)
For people who always keep ending up on this page, like I did: I found a very hacky solution to remove an element from a list<QtObject>.
myList = Array.from(myList).filter(r => r !== elementIWant2Remove)
This is not very elegant, but it does the trick, if you need to remove an element.
You can't remove individual items. According to the docs:
Note that objects cannot be individually added to or removed from the list once created; to modify the contents of a list, it must be reassigned to a new list.
This means that the pop method most likely won't exist with the basic list type. (However, there is a push method.)
You'll be much better off using JavaScript arrays.
property var myArray: [1, 2, 3]
Component.onCompleted: {
myArray.push(4);
myArray.push(5);
myArray.splice(1, 1) // simulates a remove
myArray.pop(); // pop last item
console.debug(myArray) // [1, 3, 4]
}
I've used ints here, but you can also store QML objects and other types as well. Using JavaScript arrays exposes your variables to most (if not all) of JavaScript's Array functions.