2

在论坛的帮助下,我动态创建了一些按钮,我为“createObject()”函数中的属性分配了一个函数。现在,在这个函数中,我想引用其他动态创建的项目。

(伪)代码目前看起来像这样:

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"
                                        });
        }
    }

最后我想做的是:为每个硬币/纸币创建按钮,当点击时,我想改变它旁边的文本内容..就像一个计数器,我点击了多少次按钮.

有没有比带着信号等走在路上更简单的方法?(看起来很复杂)

谢谢你的时间和帮助-m

4

1 回答 1

2

您使用将对象实例传递给按钮操作。例如:

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.

于 2013-01-17T05:27:28.127 回答