Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    [SOLVED] Iteration of QML elements

    QML and Qt Quick
    3
    4
    2693
    Loading More Posts
    • 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
      vamfirius last edited by

      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 Reply Quote 0
      • T
        task_struct last edited by

        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 Reply Quote 0
        • C
          chriadam last edited by

          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 Reply Quote 0
          • V
            vamfirius last edited by

            task_struct

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

            1 Reply Last reply Reply Quote 0
            • First post
              Last post