Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] Iteration of QML elements
QtWS25 Last Chance

[SOLVED] Iteration of QML elements

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 3.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vamfirius
    wrote on last edited by
    #1

    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 -

    @
    for(i=0;i<10;i++){

    /->code to iterate/
    Rectangle{
    width: 59
    height: 80
    border.color: "#000"
    Image {
    fillMode: Image.PreserveAspectFit
    source: i+".png"
    }
    MouseArea{
    anchors.fill:parent
    onEntered:parent.border.color = "#ffffff"
    onExited:parent.border.color = "#000000"
    onClicked:{
    if(parent.opacity == 1)parent.opacity = 0.5
    else parent.opacity = 1
    onClicked:LibJs.updateDeckMemResults(i)
    }
    }
    }
    /<-code to iterate/

    }

    @

    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 didnt help, but may be I didnt asked right question...

    1 Reply Last reply
    0
    • T Offline
      T Offline
      task_struct
      wrote on last edited by
      #2

      Hello,

      If I understand you right, you want to create 10 Rectangles. You could use "Repeater":http://doc.qt.nokia.com/4.7-snapshot/qml-repeater.html 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."

      • Linu...
      1 Reply Last reply
      0
      • C Offline
        C Offline
        chriadam
        wrote on last edited by
        #3

        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 (http://doc.qt.nokia.com/5.0-snapshot/qml-qtquick2-repeater.html).

        1 Reply Last reply
        0
        • V Offline
          V Offline
          vamfirius
          wrote on last edited by
          #4

          task_struct

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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved