访问父属性无效(QML)
Accessing parent's properties not working (QML)
我有一个包含 Item
的 EntityBase
(Player
),其中包含一个动态加载项目 (Modules
) 的 Loader
。
现在我需要访问 Modules
中 Player
的属性。
正在打印 (console.debug
) Module
的 parent.parent.parent
输出
Player_QMLTYPE_127(0x2ac147f0)
所以我认为我可以通过编写 parent.parent.parent.myProperty
来访问 Player
的属性,但是 none 我的属性或子元素出现在自动完成列表中。
有什么我想念的吗?或者这根本不可能?
我想在这种情况下,最好的方法是使用 signal
s 和 Connection
s 从组件内部向最外层的项目进行通信,如 here.[=25 所述=]
它遵循从文档中复制的示例(其中 MyItem.qml
是您动态加载的项目):
import QtQuick 2.0
Item {
width: 100
height: 100
Loader {
id: myLoader
source: "MyItem.qml"
}
Connections {
target: myLoader.item
onMessage: console.log(msg)
}
}
无论如何,文档还确认:
Alternatively, since MyItem.qml is loaded within the scope of the Loader, it could also directly call any function defined in the Loader or its parent Item.
如果我正确理解了这个问题,这与您实际尝试做的事情类似,但我发现它限制了项目的可重用性,因为它对周围环境做出了假设,而我通常不这样做喜欢(当然这是我的设计理念,不装最好)
另请注意,Loader
的 parent 属性 指向其视觉父级,这可能与您期望的有所不同。那是因为它实际上是一个 Item
并且它继承了它的 parent 属性.
有关详细信息,请参阅 here。
相反,要在创建的加载项目上设置属性,而不是通过 parent.parent...
链访问它们,您可以将这些属性放在 Loader
对象上并访问它们如下所示很容易(这是从文档中提取的另一个示例):
Item {
width: 400
height: 400
Component {
id: myComponent
Text { text: modelIndex }
}
ListView {
anchors.fill: parent
model: 5
delegate: Component {
Loader {
property int modelIndex: index
sourceComponent: myComponent
}
}
}
}
事实上,如文档中所述,您可以:
[...] explicitly set the required information as a property of the Loader (this works because the Loader sets itself as the context object for the component it is loading).
关于组件的作用域,从here可以看出:
The component scope is the union of the object ids within the component and the component's root object's properties.
我有一个包含 Item
的 EntityBase
(Player
),其中包含一个动态加载项目 (Modules
) 的 Loader
。
现在我需要访问 Modules
中 Player
的属性。
正在打印 (console.debug
) Module
的 parent.parent.parent
输出
Player_QMLTYPE_127(0x2ac147f0)
所以我认为我可以通过编写 parent.parent.parent.myProperty
来访问 Player
的属性,但是 none 我的属性或子元素出现在自动完成列表中。
有什么我想念的吗?或者这根本不可能?
我想在这种情况下,最好的方法是使用 signal
s 和 Connection
s 从组件内部向最外层的项目进行通信,如 here.[=25 所述=]
它遵循从文档中复制的示例(其中 MyItem.qml
是您动态加载的项目):
import QtQuick 2.0
Item {
width: 100
height: 100
Loader {
id: myLoader
source: "MyItem.qml"
}
Connections {
target: myLoader.item
onMessage: console.log(msg)
}
}
无论如何,文档还确认:
Alternatively, since MyItem.qml is loaded within the scope of the Loader, it could also directly call any function defined in the Loader or its parent Item.
如果我正确理解了这个问题,这与您实际尝试做的事情类似,但我发现它限制了项目的可重用性,因为它对周围环境做出了假设,而我通常不这样做喜欢(当然这是我的设计理念,不装最好)
另请注意,Loader
的 parent 属性 指向其视觉父级,这可能与您期望的有所不同。那是因为它实际上是一个 Item
并且它继承了它的 parent 属性.
有关详细信息,请参阅 here。
相反,要在创建的加载项目上设置属性,而不是通过 parent.parent...
链访问它们,您可以将这些属性放在 Loader
对象上并访问它们如下所示很容易(这是从文档中提取的另一个示例):
Item {
width: 400
height: 400
Component {
id: myComponent
Text { text: modelIndex }
}
ListView {
anchors.fill: parent
model: 5
delegate: Component {
Loader {
property int modelIndex: index
sourceComponent: myComponent
}
}
}
}
事实上,如文档中所述,您可以:
[...] explicitly set the required information as a property of the Loader (this works because the Loader sets itself as the context object for the component it is loading).
关于组件的作用域,从here可以看出:
The component scope is the union of the object ids within the component and the component's root object's properties.