3

I want to create a QML binding in a repeated component. I want to bind the value of one of the elements to a property from a known object. 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?

PS. If possible I guess I could pass the property directly to the repeater but then I would like to be able to convert the property to a string because I need both and don't want to pass both.

EDIT: Here's what I want:

    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
        }
    }
6
  • Show us what you tried so far. Commented Apr 3, 2016 at 15:15
  • @ddriver I haven't actually tried anything because I can't think of anything that might even work Commented Apr 3, 2016 at 15:31
  • It doesn't have to work, it just as to illustrate intent and effort. Commented Apr 3, 2016 at 15:34
  • @ddriver ok, I've added the type of thing that I want Commented Apr 3, 2016 at 15:40
  • What is tBox and settings? Commented Apr 3, 2016 at 15:48

1 Answer 1

6

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?

If you look at the documentation for Binding you will discover that the property property expects a string - property : string

So you don't have anything to resolve, that happens internally.

my problem is the "value: settings.setting" line

You could try something like settings[setting]

Sign up to request clarification or add additional context in comments.

5 Comments

Yes at the one end that works but my problem is the "value: settings.setting" line on the other binding
Thanks, that's exactly what I needed!
@Gerharddc - if you plan on using QML extensively, you might want to learn the basics of JavaScript. It will save you lots of time. Your problem was just standard JS stuff.
So was I, but the last couple of years I dare say I replaced about 90% of the C++ with JS, much easier and faster to prototype. I just focus on a flexible and extensible C++ core and do most of the stuff in QML. It really helps when you don't need to recompile for every single tiny change, and you don't get hard crashes on bugs :)
I'll keep that in mind should I one day become a permanent Qt user

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.