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; }], 
                                       ...});

とにかく、大量のオブジェクトを動的に作成するにはRepeater要素を使用することをお勧めします。

于 2013-01-17T05:27:28.127 に答える