【发布时间】:2021-08-01 15:59:12
【问题描述】:
在我的 python & qml 项目中,我的页面结构如下
Window
|-myObject
|-Loader
|-homePage
|-myComponent
其中 MyComponent 是我使用以下 Python 代码动态创建的组件:
root = engine.rootObjects()[0].findChild(QObject, "HomePage")
componentFigure = QQmlComponent(engine)
componentFigure.loadUrl(QUrl("components/MyComponent.qml"))
instanceFigure = componentFigure.create()
instanceFigure.setParentItem(root)
现在我想访问 myObject 属性,从 HomePage 可以正常工作,从 myComponent 它不工作(无法读取 PropertyName of undefined)
我的组件创建可能有问题?
根页面代码:
Window {
Settings {
id: appSettings
property bool light: true
}
QtObject {
id: myObject
property color myColor: {appSettings.mySetting ? "#FFFFFF" : "#000000"}
}
Rectangle {
anchors.fill: parent
Loader {
anchors.fill: parent
source: Qt.resolvedUrl("pages/homePage.qml")
}
}
}
我的组件代码:
MyComponent{
anchors.fill: parent
Rectangle {
anchors.fill: parent
color: myObject.myColor
}
}
【问题讨论】:
-
请添加您的代码,以便我们了解您是如何尝试进行这些引用的。
标签: python pyqt qml pyside pyside2