2012-11-07 2 views
11

아래 코드는 imgx 요소 속성을 변경할 수있는 방법을 알려줍니다. 자바 스크립트를 사용하여 imgx.x 값을 변경해야합니다. 아니면 다른 방법이 있습니까? qt 문서를 검색하지만 도움이되지는 않습니다. 감사.QML로 리피터의 자식 속성에 액세스하는 방법은 무엇입니까?

Row { 
    Repeater { 
     id:mmm 
     model : 10 
     Rectangle{ 
      clip: true 
      width: 54 
      height: 80 
      color:"transparent" 
      Image { 
       id:imgx 
       //x:-160 
       //x:-105 
       //x:-50 
       x:0 
       source: "images/tarama_lights.png" 
      } 
     } 
    } 
} 

답변

18

당신은 리피터 (귀하의 경우 사각형)의 직접 아이에 속성을 추가하고 (귀하의 경우 이미지)를 내부 아이의 속성에 대한 목표로 설정해야합니다. 그런 다음 mmm.itemAt(<index of the element>).<property> = value을 사용할 수 있습니다. 코드 :

Repeater { 
    id:mmm 
    model : 10 
    Rectangle{ 
    clip: true 
    width: 54 
    height: 80 
    color:"transparent" 
    property int imageX: 0 //adding property here 

    Image { 
     id:imgx 
     x: parent.imageX //setting property as the target 
     source: "images/tarama_lights.png" 
    } 
    } 
} 

당신은 다음과 같은 속성을 변경할 수 있습니다

onPropertyChange: { 
    mmm.itemAt(index).imageX = newValue //the index defines which rectangle you change 
} 
+1

감사합니다. 그것은 매우 도움이됩니다. –

+0

@JuliusG 클릭 한 항목의 인덱스는 어떻게 얻을 수 있습니까? – MrPickles

+1

@MrPickles는 childAt (...) 메소드를 살펴 봅니다. http://doc.qt.io/qt-5/qml-qtquick-item.html#childAt-method – JuliusG

2

JuliusG의 대답은 바로 itemAt를 사용하여입니다. 그러나 내부 아동의 재산에 대한 목표로 설정할 필요는 없습니다 (귀하의 경우 이미지). 이 대신이 사용

onPropertyChange: { mmm.itemAt(index).imageX = newValue //the index defines which rectangle you change } 

이다과 같이 이 코드를 가질 수 있습니다
onPropertyChange: { mmm.itemAt(index).children[0].x = newValue //the index defines which rectangle you change } 

는 도움이되기를 바랍니다.

관련 문제