March 21, 2012

vamfirius vamfirius
Lab Rat
7 posts

[SOLVED] Iteration of QML elements


Hi everyone.

How can I iterate a part of qml code with a help of js or c++ maybe.
That,s what I want to do –

  1. for(i=0;i<10;i++){
  3. /*->code to iterate*/
  4.  Rectangle{
  5.         width: 59
  6.         height: 80
  7.         border.color: "#000"
  8.         Image {
  9.             fillMode: Image.PreserveAspectFit
  10.             source: i+".png"
  11.         }
  12.         MouseArea{
  13.             anchors.fill:parent
  14.             onEntered:parent.border.color = "#ffffff"
  15.             onExited:parent.border.color = "#000000"
  16.             onClicked:{
  17.                 if(parent.opacity == 1)parent.opacity = 0.5
  18.                 else parent.opacity = 1
  19.                 onClicked:LibJs.updateDeckMemResults(i)
  20.             }
  21.         }
  22.     }
  23. /*<-code to iterate*/
  25. }

I thought about creating objects from .qml file in a cycle in js but in this case I can,t use LibJs object because of variables in js will be redifined each time I call LibJs.updateDeckMemResults(i). Google didn`t help, but may be I didn`t asked right question…

3 replies

March 21, 2012

task_struct task_struct
Ant Farmer
350 posts


If I understand you right, you want to create 10 Rectangles. You could use Repeater [] and it`s property index


“Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program.”
- Linus Torvalds

March 21, 2012

chriadam chriadam
Lab Rat
178 posts

You basically want to dynamically generate 10 instances of a Component (the Rectangle).
You should split it out into a separate .qml file (eg, MyRect.qml), and then you can use Qt.createComponent() and Qt.createObject() to construct instances dynamically.

Alternatively, you could use a Repeater or something (

March 21, 2012

vamfirius vamfirius
Lab Rat
7 posts


Thank you very much – this is perfect solution in my case.

  ‹‹ [SOLVED] Adding items into array in qml script      [Solved] HapticsEffect does not do what I want ››

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