Repeater and itemAt issue
Hello, I am experimenting with QML and trying to access an item created by repeator, but am getting the error:
TypeError: Result of expression ‘repeater.itemAt(0)’ [null] is not an object.
I can access the repeater.count but not any items.
How can I do this?
- import QtQuick 1.1
- Item {
- width: parent.width
- height: parent.height
- Row {
- id: rows
- Repeater {
- id: repeater
- model: 10
- Rectangle {
- width: 10; height: 20;
- }
- }
- }
- Rectangle {
- x: repeater.itemAt(0).x // this fails with TypeError: Result of expression 'repeater.itemAt(0)' [null] is not an object.
- y: 100 * repeater.count // this works
- width: 10; height: 10
- }
- }
2 replies
Hi,
There are a couple issues at play here:
1. The order in which the bindings are initially evaluated. In this case, repeater.itemAt(0).x is evaluated before the Repeater has actually created its children.
2. The fact that methods can’t NOTIFY (like properties do) when their return value would change. In this case, even when the items are finally created, repeater.itemAt(0).x is not reevaluated.
You could work around this by assigning the value (or binding) at component completion instead, e.g.
- Component.onCompleted: myRect.x = repeater.itemAt(0).x //once-off assignment
or
- Component.onCompleted: myRect.x = (function(){ return repeater.itemAt(0).x; }) //binding
Regards,
Michael
You must log in to post a reply. Not a member yet? Register here!



