December 29, 2011

babazaroni babazaroni
Lab Rat
63 posts

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?

  1. import QtQuick 1.1
  2.  
  3. Item {
  4.     width: parent.width
  5.     height: parent.height
  6.  
  7.     Row {
  8.         id: rows
  9.         Repeater {
  10.             id: repeater
  11.             model: 10
  12.             Rectangle {
  13.                   width: 10; height: 20;
  14.           }
  15.         }
  16.     }
  17.     Rectangle {
  18.         x: repeater.itemAt(0).x  // this fails with TypeError: Result of expression 'repeater.itemAt(0)' [null] is not an object.
  19.         y: 100 * repeater.count  // this works
  20.         width: 10; height: 10
  21.     }
  22. }

2 replies

December 30, 2011

ixSci ixSci
Lab Rat
203 posts

Hi babazaroni! Repeater is not an actual parent of the items but the repeater’s parent is. Repeater just adds children to the positioner item so you should use your Row with rows id to work with children.

December 30, 2011

mbrasser mbrasser
Ant Farmer
452 posts

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.

  1. Component.onCompleted: myRect.x = repeater.itemAt(0).x //once-off assignment

or

  1. Component.onCompleted: myRect.x = (function(){ return repeater.itemAt(0).x; }) //binding

Regards,
Michael

 
  ‹‹ XmlListModel - Getting XML data      [Solved] Looking for an aready installed application ››

You must log in to post a reply. Not a member yet? Register here!