Qt Qml-在两个组件之间传递属性值
这是我在main.qml文件中处理的情况的简化:Qt Qml-在两个组件之间传递属性值,qt,properties,qml,components,Qt,Properties,Qml,Components,这是我在main.qml文件中处理的情况的简化: Component { id: component1 property string stringIneedToPass: "Hello" Text { text: stringIneedToPass } } Component { id: component2 Rectangle { id: myRectangle property string stringIneedToReceive = component1.string
Component {
id: component1
property string stringIneedToPass: "Hello"
Text { text: stringIneedToPass }
}
Component {
id: component2
Rectangle {
id: myRectangle
property string stringIneedToReceive = component1.stringIneedToPass; //this doesn't work
}
}
显然我的情况更复杂。但最终我只需要了解这种转移应该如何进行
谢谢大家! 首先,
组件
元素不能有属性<代码>组件要么从文件加载,要么以声明方式定义,在后一种情况下,它们只能包含一个根元素和一个id
第二,不能在元素体中进行赋值,只能在绑定中进行赋值
第三,您不能从外部引用组件内部元素中定义的属性,因为在实例化组件之前,该对象不存在。此类对象只能从内部引用
除此之外,它将按预期工作,如果您可以引用它,您可以根据需要将其绑定或分配给属性
因此,您只需将字符串属性设置为外部:
property string stringIneedToPass: "Hello"
Component {
id: component1
Text {
text: stringIneedToPass
}
}
Component {
id: component2
Rectangle {
id: myRectangle
property string stringIneedToReceive: stringIneedToPass
}
}
正如@dtech指出的,您的代码存在各种问题。您只能在实例化时分配或绑定组件对象的属性。为了提供更好的方法,您应该提供详细信息,说明您计划如何实例化组件。