Qt 如何访问随机中继器的属性';谁的孩子?

Qt 如何访问随机中继器的属性';谁的孩子?,qt,qml,Qt,Qml,我有一个嵌套元素的网格(矩形内的文本和鼠标区域): 我想随机更改网格中一些方块和文本的颜色,但我不知道如何访问它们,我尝试使用如下计时器: Timer { id: alfa interval: 500; running: true; repeat: true onTriggered: { /*if random square not white , a color from color array is picked to

我有一个嵌套元素的网格(矩形内的文本和鼠标区域):

我想随机更改网格中一些方块和文本的颜色,但我不知道如何访问它们,我尝试使用如下计时器:

Timer {
        id: alfa
        interval: 500; running: true; repeat: true
        onTriggered: {

            /*if random square not white , a color from color array is picked to change it
            else random square.color = "white"*/
        }

    }
    Timer {
        id: beta
        interval: 1000; running: true; repeat: true
        onTriggered: {
           //changes the text of a random tttt element in the grid
        }
    } 

我尝试了很多方法,但都失败了,比如使用属性绑定似乎改变了整个网格的颜色和文本,而不是一个正方形,我根本不理解如何访问嵌套元素和重复子元素,文档也帮不上我该怎么做?

因为他们想修改视图显示的信息,您不应直接与它交互,而应创建一个模型,并通过它修改数据:

property variant colorArray: ["#00bde3", "#67c111", "#ea7025"]

ListModel{
    id: mymodel
    Component.onCompleted: {
        for(var i=0; i<25; i++){
            mymodel.append({"text": ""+i, "color": "white"})
        }
    }
}
Grid{
    rows: 5
    columns: 5
    spacing: 5
    anchors.centerIn: parent
    Repeater{
        id: gridRect
        model: mymodel
        Rectangle{
            id: rect
            width: 50
            height: width
            color: model.color
            radius: 5
            Text {
                id: tttt
                anchors.centerIn: parent
                color: "lightBlue"
                text : model.text
            }
            MouseArea{
                 anchors.fill: parent
            }
        }
    }
}
Timer{
    id: alfa
    interval: 500; running: true; repeat: true
    onTriggered: {
        var random_color = colorArray[Math.floor(Math.random() * colorArray.length)]
        var random_ix = Math.floor(Math.random() * mymodel.count);
        var elem = mymodel.get(random_ix)
        elem.color = elem.color === "white" ? random_color : "white"
    }
}
Timer{
    id: beta
    interval: 1000; running: true; repeat: true
    onTriggered: {
        // https://stackoverflow.com/a/38620178
        var random_str = "";
        var alphabet = "abcdefghijklmnopqrstuvwxyz";
        while (random_str.length < 6) {
            random_str += alphabet[Math.floor(Math.random() * alphabet.length)];
        }
        var random_ix = Math.floor(Math.random() * mymodel.count);
        var elem = mymodel.get(random_ix)
        elem.text = random_str
    }
}
属性变量colorArray:[“#00bde3”、“#67c111”、“#ea7025”]
列表模型{
id:mymodel
Component.onCompleted:{

对于(var i=0;i您可以想象中继器的工作方式就像您刚刚将该矩形复制到网格中25次一样。这意味着为了访问其中一个矩形,您必须访问网格的子级

您可以使用eyllanesc先前的响应,并在ListModel中编辑数据(我认为这是正确的方法)

或者,您也可以使用网格中应该存在的元素,它返回网格拥有的子元素数组,即您通过Repeater添加的矩形

我个人会用ListModel的方式来做这件事

property variant colorArray: ["#00bde3", "#67c111", "#ea7025"]

ListModel{
    id: mymodel
    Component.onCompleted: {
        for(var i=0; i<25; i++){
            mymodel.append({"text": ""+i, "color": "white"})
        }
    }
}
Grid{
    rows: 5
    columns: 5
    spacing: 5
    anchors.centerIn: parent
    Repeater{
        id: gridRect
        model: mymodel
        Rectangle{
            id: rect
            width: 50
            height: width
            color: model.color
            radius: 5
            Text {
                id: tttt
                anchors.centerIn: parent
                color: "lightBlue"
                text : model.text
            }
            MouseArea{
                 anchors.fill: parent
            }
        }
    }
}
Timer{
    id: alfa
    interval: 500; running: true; repeat: true
    onTriggered: {
        var random_color = colorArray[Math.floor(Math.random() * colorArray.length)]
        var random_ix = Math.floor(Math.random() * mymodel.count);
        var elem = mymodel.get(random_ix)
        elem.color = elem.color === "white" ? random_color : "white"
    }
}
Timer{
    id: beta
    interval: 1000; running: true; repeat: true
    onTriggered: {
        // https://stackoverflow.com/a/38620178
        var random_str = "";
        var alphabet = "abcdefghijklmnopqrstuvwxyz";
        while (random_str.length < 6) {
            random_str += alphabet[Math.floor(Math.random() * alphabet.length)];
        }
        var random_ix = Math.floor(Math.random() * mymodel.count);
        var elem = mymodel.get(random_ix)
        elem.text = random_str
    }
}