21

你能告诉我下面的代码有没有办法改变 imgx 元素的属性。我必须使用 javascript 更改 imgx.x 值。或者还有其他方法吗?我搜索 qt 文档但没有帮助。谢谢。

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"
            }
        }
    }
}
4

2 回答 2

28

您必须将属性添加到中继器的直接子级(在您的情况下为矩形)并将其设置为内部子级中的属性的目标(在您的情况下为图像)。然后您可以使用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
}
于 2012-11-07T15:20:03.307 回答
6

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 }

希望能帮助到你。

于 2016-05-10T09:52:15.460 回答