Qt 如何将项添加到阵列qml?
我有: MyItem.qmlQt 如何将项添加到阵列qml?,qt,qml,Qt,Qml,我有: MyItem.qml import QtQuick 2.0 Item { property string info1: "info" property int info2: 1 } 如何在初始化期间将项添加到阵列qml 这是我的工作: import QtQuick 2.0 Item { property var arr: [{ info1: "test", info2: 1}, { info1: "info" }] } 但是,这是行不通的: imp
import QtQuick 2.0
Item {
property string info1: "info"
property int info2: 1
}
如何在初始化期间将项添加到阵列qml
这是我的工作:
import QtQuick 2.0
Item {
property var arr: [{ info1: "test", info2: 1}, { info1: "info" }]
}
但是,这是行不通的:
import QtQuick 2.0
Item {
property var arr: [MyItem { info1: "test", info2: 1}, MyItem { info1: "info" }]
}
您可以尝试使用:
有关更多信息,请参阅和。如果使用较新的QML版本,则可以使用新的
列表
-类型作为属性。在那里,您可以轻松地以所需的语法添加项,就像使用属性别名someProperty:someItem.somePropertyList(例如,children)
时一样
include QtQuick 2.10 // I think 2.9 is sufficient
ApplicationWindow {
...
property list<Item> myItemList: [
Item { Component.onCreated: console.log(parent) }, // parent will be null! Set it explicitly if needed.
Item {},
Item {},
...
]
}
然后您可以使用它:
ArrayObject {
id: tst
Item {
}
Item {
}
Item {
}
}
当然,可能还有其他解决办法
例如:
如果可以将
项作为数组进行访问,则您可能正在查找所有子项。你必须澄清你的问题才能得到答案。你为什么需要它?@eyllanesc:为什么?父级也可以是null
import QtQuick 2.0
QtObject {
id: root
default property QtObject store
readonly property var arr: (arr === undefined ? [] : arr) // This will give you a binding loop, but ensures that after filling the array, it is not reset to [].
onStoreChanged: {
if (store) {
console.log(store)
arr[arr.length] = store
console.log(arr, arr.length)
}
store = null
}
}
ArrayObject {
id: tst
Item {
}
Item {
}
Item {
}
}
Item {
property var myArray: [
itemId1, itemId2, itemId3, itemId4
]
Item { id: itemId1 }
...
}