2015-07-16 2 views
1

У меня есть следующие 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?

Благодаря

ответ

4

Использование Repeater «s 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 для создания капли дождя.

+0

Я надеялся, что будет более декларативный способ итерации детей, например 'for (r в rainDropId.items) {r.x = ....; r.y = ...} ' – xtofl

Смежные вопросы