2012-11-07 68 views
11

imgx要素のプロパティを変更する方法があるので、下のコードについて教えてください。私はjavascriptを使用してimgx.xの値を変更する必要があります。それとも他の方法がありますか? Qt Docsを検索しますが、役に立たないものは検索しません。ありがとう。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 } 

はそれがお役に立てば幸いです。

関連する問題