4x10
4x10

Reputation: 43

QML - Intercation between 2 dynamically created Elements

With the help of the forum I dynamically created some buttons which I assigned a function to a property within the "createObject()" function.. Now, inside this function, I would like to refer to other Items which are also dynamically created.

The (pseudo)code currently looks something like this:

property var money_back: { '50e': 0,
                           '20e': 0,
                           '10e': 0,
                           '5e': 0,
                           '2e': 0,
                           '1e': 0,
                           '50c': 0,
                           '20c': 0,
                           '10c': 0,
                           '5c': 0,
                           '2c': 0,
                           '1c': 0 };
Row{
    id:money_row
    spacing: 5

    Component.onCompleted: {
        var button = Qt.createComponent("BubbleButton.qml");
        var selected = Qt.createComponent("ChangeText.qml");
        for (var prop in change_screen.money_back){
            selected.createObject(money_row,{
                                      "id": "selected_"+prop,
                                      "selected": "0"
                                    });
            button.createObject(money_row,{
                                            "id": "button_"+prop,
                                            // for testing purposes I wanted to make at least the first button work.. of course i want something like 'selected_+prop.selected'
                                            "action": [function(){ selected_50e.selected += 1; }], 
                                            //"ps": ps,
                                            "img_id.source": prop+".png",
                                            "img_id.align": "center",
                                            "color": "transparent"
                                        });
        }
    }

What I want to do in the end is the following: create Buttons for each coin/note and when clicked on, I want to change the content of the Text next to it.. like a counter, how many times I clicked on a Button.

Is there an easier way than going down the road with signals and such? (looks complicated)

thank you for your time and help -m

Upvotes: 3

Views: 806

Answers (1)

Dickson
Dickson

Reputation: 1261

You use pass the object instance to the button action. For example:

Component.onCompleted: {
    ...
    for (var prop in change_screen.money_back){
        var selectedObject = selected.createObject(...);
        button.createObject(money_row,{...,
                                       "action": [function(){ selectedObject.selected += 1; }], 
                                       ...});

Anyway, I recommend you to use the Repeater element to dynamically create such huge amount of object.

Upvotes: 2

Related Questions