December 23, 2010

ixSci ixSci
Lab Rat
203 posts

Get Repeater’s child

 

Hello folks, I’m just wondering how I can change the visual element which has been created by the Repeater or other “Positioner” elements. I have the following QML:

  1. Repeater{
  2.         model: buttunGroup.buttonList
  3.         RadioButton{
  4.             title: buttonText
  5.         }
  6.     }

After it has been created I want to change some property of one of my “RadioButton” elements but I find no way to accomplish that. I’ve found the children property but have no clue how to dissect a list I retrieved from that property.
Is there a way to do that?

11 replies

December 23, 2010

Kxyu Kxyu
Lab Rat
145 posts

you can make an array in .js file and add all children in it using onCompleted signal

December 23, 2010

ixSci ixSci
Lab Rat
203 posts

Kxyu, thanks for the answer!
No options to achieve it without wielding JS?
It is not that I ‘ve something against JS. I just want to have a pure QML solution for this, if any.
There is no way to acquire children from the list, is there?

December 23, 2010

Kxyu Kxyu
Lab Rat
145 posts

you can, actually I figured it out) i found the following string in Repeater description:
Items instantiated by the Repeater are inserted, in order, as children of the Repeater’s parent

So you can access the children like this:

  1. import Qt 4.7
  2.  
  3.  
  4. Rectangle {
  5.     id: screen
  6.     width: 1024
  7.     height: 600
  8.  
  9. Row{
  10. id:container
  11. Repeater{
  12. model:3
  13. delegate:Rectangle{
  14. width:100
  15. height:100
  16. color:"red"
  17. }
  18.  
  19. }
  20. }
  21.  
  22.  
  23. Component.onCompleted:container.children[1].color="black"
  24.  
  25.  
  26. }

it works!

December 24, 2010

ixSci ixSci
Lab Rat
203 posts

Kxyu, it is exactly what I’ve searched for. Thank you for the solution.
Though I don’t understand why the parent item doesn’t keep its children amount. Maybe it should? It seems a bit awkward to retrieve children from a Row and get children number from a Repeater :)

Nevertheless, the question is answered.

December 24, 2010

Kxyu Kxyu
Lab Rat
145 posts

Well it keeps, as children is a List, and List does. But as far as I understand in QML it works like javascript array, so you can use it’s length property. I don’t know why, but it gives amount+1, still you can use it

December 24, 2010

xsacha xsacha
Lab Rat
517 posts

Well the children array is 0-index. So 0 is the first child. However, a length property will be 1-index.
If you’ve already accounted for this, it could be that the 0th child is pre-populated (some hidden child?). I haven’t tested this though.

 Signature 

- Sacha

December 24, 2010

ixSci ixSci
Lab Rat
203 posts

Kxyu wrote:
Well it keeps, as children is a List, and List does. But as far as I understand in QML it works like javascript array, so you can use it’s length property. I don’t know why, but it gives amount+1, still you can use it

Interesting property it should be documented I believe. Because I didn’t find any mentions about it neither in documentation nor in the IntelliSense in QtCreator.

December 24, 2010

ixSci ixSci
Lab Rat
203 posts

Just tested it: a Repeater is the rightful child of an Item that is why you have amount + 1 result. If you exclude Repeater from the item you will have an actual amount.

December 24, 2010

xsacha xsacha
Lab Rat
517 posts

IntelliSense needs some updates. It’s not mentioned in documentation probably because it isn’t related to QML. Just as the Math.functions aren’t mentioned. Basically, javascript works in QML.

 Signature 

- Sacha

December 24, 2010

ixSci ixSci
Lab Rat
203 posts

But if the QML list is nothing more than JS list why there is not a single word about it? How can user find that he can use length property, for example, if he has no clue about a nature of a QML list?

December 24, 2010

Kxyu Kxyu
Lab Rat
145 posts
xsacha wrote:
IntelliSense needs some updates. It’s not mentioned in documentation probably because it isn’t related to QML. Just as the Math.functions aren’t mentioned. Basically, javascript works in QML.

well, that’s correct, but I think it would be better, if list in QML had all QList members and worked the same way, not like javascript array

 
  ‹‹ Qt mobility 1.1.0 with Qt 4.7.0 help      Animating button press ››

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