gpt4 book ai didi

qt - QML 通过属性名称字符串访问对象属性

转载 作者:行者123 更新时间:2023-12-04 00:10:32 27 4
gpt4 key购买 nike

我想在重复的组件中创建一个 QML 绑定(bind)。我想将其中一个元素的值绑定(bind)到已知对象的属性。我的问题是我要绑定(bind)的所述属性的名称将作为组件中的字符串提供。如何将属性名称解析为可用作绑定(bind)值的实际属性?

附言。如果可能的话,我想我可以将属性直接传递给中继器,但我希望能够将属性转换为字符串,因为我需要两者并且不想传递两者。

编辑:这是我想要的:

    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]

关于qt - QML 通过属性名称字符串访问对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36387130/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com