gpt4 book ai didi

qt - 如何从 QML 基本类型列表中动态删除元素?

转载 作者:行者123 更新时间:2023-12-04 01:15:52 28 4
gpt4 key购买 nike

我已成功添加元素使用

list.push(element)

但是我如何删除它们?我尝试了以下方法,但它们似乎都不起作用。
list.pop()
list.pop_front()
list.remove()
list.remove(int)
list.remove(element)

最佳答案

您无法删除单个项目。根据 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.



这意味着 pop 方法很可能不会与基本列表类型一起存在。 (但是,有一个 push 方法。)

使用 JavaScript 数组会好得多。
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]
}

我在这里使用了 int ,但您也可以存储 QML 对象和其他类型。使用 JavaScript 数组会将您的变量暴露给大多数(如果不是全部)JavaScript 的 Array functions

关于qt - 如何从 QML 基本类型列表中动态删除元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54149331/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com