我有一个容器,它在运行时被动态创建的组件填充。这些元素中的每一个(我们称它们为DynamicObject
s)都有一个子元素树(Node
s),也是动态创建的。更重要的是,Node
组件可以相互嵌套(如树结构)。
让容器将id
参数设置为"main_container"
并且每个容器都将DynamicObject
其设置为"dynamic_object"
。
每当我尝试main_container
从任何嵌套Node
元素中解决问题时,它都可以正常工作。问题是当我尝试dynamic_object
从根以外的任何Node
其他人(那个是 的直接后代/孩子dynamic_object
)中解决时。结果是:
ReferenceError:动态对象未定义
我的问题是:这种行为背后的原因可能是什么?它与这些对象是动态创建的事实有关吗?(这是我的第一个想法,因为我总是可以解决它,main_container
并且它是在 qml 代码中静态声明的)。
代码示例:(如果有任何遗漏,请在评论中告诉我)
// Container.qml
import "container_logic.js" as Logic
Rectangle {
id: main_container
Keys.onTabPressed: {
Logic.createComponent("DynamicObject.qml", {/* some parameters */});
var dynamic_object = Logic.object;
Logic.createComponent("Node.qml",{labelText: "asdefg"}, undefined, dynamic_object)
var asdefg = Logic.object;
Logic.createComponent("Node.qml",{labelText: "tree A"}, undefined, dynamic_object)
var tree_a = Logic.object;
Logic.createComponent("Node.qml",{labelText: "a"}, undefined, asdefg)
var a = Logic.object;
Logic.createComponent("Node.qml",{labelText: "s"}, undefined, asdefg)
var s = Logic.object;
asdefg.subnodes = [a, s]
Logic.createComponent("Node.qml",{labelText: "tree B", isInput: false}, undefined, dynamic_object)
var tree_b = Logic.object;
Logic.createComponent("Node.qml",{labelText: "xyz", isInput: false}, undefined, dynamic_object)
var xyz = Logic.object;
Logic.createComponent("Node.qml",{labelText: "x", isInput: false}, undefined, xyz)
var x = Logic.object;
Logic.createComponent("Node.qml",{labelText: "y", isInput: false}, undefined, xyz)
var y = Logic.object;
Logic.createComponent("Node.qml",{labelText: "z", isInput: false}, undefined, xyz)
var z = Logic.object;
xyz.subnodes = [x,y,z]
dynamic_object.treeLeft = [asdefg, tree_a]
dynamic_object.treeRight = [tree_b, xyz]
}
}
// DynamicObject.qml
Rectangle {
id: dynamic_object
property alias treeLeft : tree_left.subnodes
property alias treeRight: tree_right.subnodes
Rectangle {
id: content_area
Node {
id: tree_left
labelText: "left"
anchors.left: parent.left
}
Node {
id: tree_right
labelText: "right"
anchors.right: parent.right
}
}
}
// Node.qml
ColumnLayout {
id: node
default property alias subnodes: subnodes_area.data
property alias labelText: label.text
Rectangle {
id: header_area
Text {
id: label
}
MouseArea {
id: mouse_area
anchors.fill: parent
hoverEnabled: true
onHoveredChanged: {
console.debug(main_container) // works fine
console.debug(dynamic_object) // **generates the error for all nodes but the root one**
}
}
}
ColumnLayout {
id: subnodes_area
anchors.top: header_area.bottom
}
}
// container_logic
var component = null
var owner = main_container
var object = null
var data = null
function createComponent(type, info, callback, container) {
callback = callback || finishComponent
owner = container || main_container
if(component != null) {
console.log("Error: a component is being loaded at this time");
return;
}
component = Qt.createComponent(type)
data = info
if(component.status === Component.Ready) {
callback()
} else if(component.status === Component.Loading) {
component.statusChanged.connect(callback)
} else {
console.log("Error loading component:", component.errorString())
}
}
function finishComponent() {
if(component.status === Component.Ready) {
object = component.createObject(owner, data)
if(object === null) {
console.log("Error creating object")
}
} else if(component.status === Component.Error) {
console.log("Error loading component:", component.errorString())
}
resetData()
}
function resetData() {
component = null;
data = null;
}