【问题标题】:How to access the properties of a Repeater's children in QML?如何在 QML 中访问中继器子级的属性?
【发布时间】:2012-10-27 13:46:20
【问题描述】:

你能告诉我下面的代码有什么方法可以改变 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"
            }
        }
    }
}

【问题讨论】:

    标签: qt qml repeater element qt-quick


    【解决方案1】:

    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 }
    

    希望对你有帮助。

    【讨论】:

    • 希望这个答案更好。
    【解决方案2】:

    您必须向Repeater 的直接子级(在您的情况下为矩形)添加一个属性,并将其设置为内部子级中的属性的目标(在您的情况下为图像)。然后您可以使用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
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    相关资源
    最近更新 更多