How do I access the children of a repeater object in a function?
-
Hi Guys,
Apologize for the newbie-question. I have some text objects in a repeater like this:
Item { id: artText Repeater { id: artTextRepeater model: 12 Text { height: 56 width: 100 color: "black" visible: index > 0 ? false : true text: "Hello " + index //x: mouse.x y: layerA.y + 22 x: 100 } } }
I want to be able to change the assigned text of all items in a function. I've tried both of these, but no luck:
function adjustText(){ for (i = 0; i < 12; i++) { artTextRepeater.itemAt(i).text = "New Text" } } function adjustText2(){ for (i = 0; i < 12; i++) { artTextRepeater.children[i].text = "New Text" } }
Sorry if this is really obvious! Thanks for the help.
-
@dxmachina Don't you need to write
var i = 0
in yourfor
loop?I've been working with your code and I think is correct. Here you have my code:
import QtQuick 2.5 import QtQuick.Window 2.2 Window { visible: true Item { id: artText Column { Repeater { id: artTextRepeater model: 12 Text { height: 56 width: 100 color: "black" visible: true text: "Hello " + index } } } } Rectangle { id: rect x: 100 y: 100 width: 100 height: 100 color: 'blue' MouseArea { anchors.fill: parent onClicked: { adjustText(artTextRepeater) } } } function adjustText(element) { for (var i = 0; i < 12; i++) { element.itemAt(i).text = "New Text" } } }
-
Welcome @dxmachina , Yes you need to define the "var type for i variable in for loop ".
Addition to above answer I will suggest below snippet where you can get rid of hard coding for model count in functionfunction adjustText(repeaterItem) {
for (var i = 0; i < repeaterItem.count; i++) {
if(repeaterItem.itemAt(i).text === ("Hello"+ i))
{
repeaterItem.itemAt(i).text = "New Text"
}
else{
repeaterItem.itemAt(i).text = "Hello"+ i
}} } Timer{ id: timer interval: 1000 running: true repeat:true onTriggered: { root.adjustText(artTextRepeater) } }
Thanks