QML Access object 属性 by 属性 name string
QML Access object property by property name string
我想在重复组件中创建 QML 绑定。我想将其中一个元素的值绑定到来自已知对象的 属性。我的问题是我要绑定的 said 属性 的名称将作为组件中的字符串提供。如何将 属性 名称解析为可用作绑定值的实际 属性?
PS。如果可能的话,我想我可以将 属性 直接传递给中继器,但是我希望能够将 属性 转换为字符串,因为我需要两者并且不想同时传递两者。
编辑:
这就是我想要的:
ListModel {
id: settingsModel
ListElement { title: "Bed Width"; setting: "bedWidth"; }
ListElement { title: "Bed Length"; setting: "bedLength"; }
}
Component {
id: settingsDelegate
Item {
width: parent.width
height: childrenRect.height
Label {
id: setLabel
text: title + ":"
width: parent.width
}
TextBox {
id: setTBox
anchors.top: setLabel.bottom
anchors.topMargin: 5
width: parent.width
Binding on text {
when: !setTBox.isActive
value: settings.setting
}
Binding {
target: settings
property: setting
value: setTBox.text
}
}
}
}
Column {
id: settingsColumn
spacing: 10
anchors.left: parent.left
anchors.right: parent.right
anchors.top: txtSave.bottom
anchors.topMargin: 15
Repeater {
model: settingsModel
delegate: settingsDelegate
}
}
My problem is that the name of said property that I want to bind to
will be provided as a string in the component. How can I resolve the
property name to an actual property that can be used as the value in a
binding?
如果您查看 the documentation for Binding
,您会发现 property
属性 需要一个字符串 - property : string
所以您没有任何需要解决的问题,这是内部发生的。
my problem is the "value: settings.setting" line
你可以试试 settings[setting]
我想在重复组件中创建 QML 绑定。我想将其中一个元素的值绑定到来自已知对象的 属性。我的问题是我要绑定的 said 属性 的名称将作为组件中的字符串提供。如何将 属性 名称解析为可用作绑定值的实际 属性?
PS。如果可能的话,我想我可以将 属性 直接传递给中继器,但是我希望能够将 属性 转换为字符串,因为我需要两者并且不想同时传递两者。
编辑: 这就是我想要的:
ListModel {
id: settingsModel
ListElement { title: "Bed Width"; setting: "bedWidth"; }
ListElement { title: "Bed Length"; setting: "bedLength"; }
}
Component {
id: settingsDelegate
Item {
width: parent.width
height: childrenRect.height
Label {
id: setLabel
text: title + ":"
width: parent.width
}
TextBox {
id: setTBox
anchors.top: setLabel.bottom
anchors.topMargin: 5
width: parent.width
Binding on text {
when: !setTBox.isActive
value: settings.setting
}
Binding {
target: settings
property: setting
value: setTBox.text
}
}
}
}
Column {
id: settingsColumn
spacing: 10
anchors.left: parent.left
anchors.right: parent.right
anchors.top: txtSave.bottom
anchors.topMargin: 15
Repeater {
model: settingsModel
delegate: settingsDelegate
}
}
My problem is that the name of said property that I want to bind to will be provided as a string in the component. How can I resolve the property name to an actual property that can be used as the value in a binding?
如果您查看 the documentation for Binding
,您会发现 property
属性 需要一个字符串 - property : string
所以您没有任何需要解决的问题,这是内部发生的。
my problem is the "value: settings.setting" line
你可以试试 settings[setting]