代码之家  ›  专栏  ›  技术社区  ›  yonutix

中继器接入元件

  •  2
  • yonutix  · 技术社区  · 9 年前

    我有以下内容 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 . 我想修改 中继器 根据它们自身的特性。我如何访问 rainDropId[index] 来自 计时器 ?

    谢谢

    1 回复  |  直到 9 年前
        1
  •  5
  •   BaCaRoZzo Indie Dev    9 年前

    使用 Repeater itemAt() 方法:

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

    您也可以考虑使用 Qt Quick Particles 以产生雨滴。