转发器访问元素

时间:2015-07-16 13:18:32

标签: qt qml repeater qtquick2

我有以下Repeater

Repeater{
    id: rainDropId
    model: 200

    delegate: Rectangle {

        x: Math.floor(Math.random() * windowId.width)
        y: Math.floor(Math.random() * windowId.height)
        property int mLayer: Math.floor(Math.random() * 4) + 1

        property double mYSpeed: -2.0 * mLayer

        width:5
        height:5
        color: "#3399FF"
        radius: width*0.5
    }
}

我有Timer。 我想根据自己的属性修改Repeater中所有元素的位置。如何从rainDropId[index]

访问Timer之类的内容

由于

1 个答案:

答案 0 :(得分:4)

使用Repeater的{​​{3}}方法:

import QtQuick 2.4
import QtQuick.Window 2.2

Window {
    id: windowId
    visible: true
    width: 400
    height: 400

    Repeater {
        id: rainDropId
        model: 200

        delegate: Rectangle {
            width:5
            height:5
            color: "#3399FF"
            radius: width*0.5
        }
    }

    Timer {
        running: true
        interval: 1000
        repeat: true
        triggeredOnStart: true
        onTriggered: {
            for (var i = 0; i < rainDropId.count; ++i) {
                rainDropId.itemAt(i).x = Math.floor(Math.random() * windowId.width);
                rainDropId.itemAt(i).y = Math.floor(Math.random() * windowId.height);
            }
        }
    }
}

您可能还会考虑使用itemAt()来创建雨滴。

相关问题