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. How do I access the children of a repeater object in a function?

How do I access the children of a repeater object in a function?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlrepeaterfunctionjavascriptchildren
5 Posts 3 Posters 9.3k 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.
  • D Offline
    D Offline
    dxmachina
    wrote on 25 Mar 2016, 19:59 last edited by
    #1

    Hi Guys,

    Apologize for the newbie-question. I have some text objects in a repeater like this:

    Item {
    	id: artText
    	Repeater {
    		id: artTextRepeater
    		model: 12
    
    		Text {
    			height: 56
    			width: 100
    			color: "black"
    			visible: index > 0 ? false : true
    			text: "Hello " + index
    			//x: mouse.x
    			y: layerA.y + 22
    			x: 100
    
    		}
    	}
    }
    

    I want to be able to change the assigned text of all items in a function. I've tried both of these, but no luck:

    function adjustText(){
    	for (i = 0; i < 12; i++) { 
    		artTextRepeater.itemAt(i).text = "New Text"
    	}
    }
    
    function adjustText2(){
    	for (i = 0; i < 12; i++) { 
    		artTextRepeater.children[i].text = "New Text"
    	}
    }
    

    Sorry if this is really obvious! Thanks for the help.

    T 1 Reply Last reply 28 Mar 2016, 09:22
    1
    • D dxmachina
      25 Mar 2016, 19:59

      Hi Guys,

      Apologize for the newbie-question. I have some text objects in a repeater like this:

      Item {
      	id: artText
      	Repeater {
      		id: artTextRepeater
      		model: 12
      
      		Text {
      			height: 56
      			width: 100
      			color: "black"
      			visible: index > 0 ? false : true
      			text: "Hello " + index
      			//x: mouse.x
      			y: layerA.y + 22
      			x: 100
      
      		}
      	}
      }
      

      I want to be able to change the assigned text of all items in a function. I've tried both of these, but no luck:

      function adjustText(){
      	for (i = 0; i < 12; i++) { 
      		artTextRepeater.itemAt(i).text = "New Text"
      	}
      }
      
      function adjustText2(){
      	for (i = 0; i < 12; i++) { 
      		artTextRepeater.children[i].text = "New Text"
      	}
      }
      

      Sorry if this is really obvious! Thanks for the help.

      T Offline
      T Offline
      tarod.net
      wrote on 28 Mar 2016, 09:22 last edited by
      #2

      @dxmachina Don't you need to write var i = 0 in your for loop?

      I've been working with your code and I think is correct. Here you have my code:

      import QtQuick 2.5
      import QtQuick.Window 2.2
      
      Window {
          visible: true
      
          Item {
              id: artText
              Column {
                  Repeater {
                      id: artTextRepeater
                      model: 12
      
                      Text {
                          height: 56
                          width: 100
                          color: "black"
                          visible: true
                          text: "Hello " + index
                      }
                  }
              }
          }
      
          Rectangle {
              id: rect
              x: 100
              y: 100
              width: 100
              height: 100
              color: 'blue'
              MouseArea {
                  anchors.fill: parent
                  onClicked: { adjustText(artTextRepeater)  }
              }
          }
      
          function adjustText(element) {
              for (var i = 0; i < 12; i++) {
                  element.itemAt(i).text = "New Text"
              }
          }
      }
      

      "Individually, we are one drop. Together, we are an ocean."

      1 Reply Last reply
      0
      • R Offline
        R Offline
        RamK
        wrote on 28 Mar 2016, 09:53 last edited by
        #3

        Welcome @dxmachina , Yes you need to define the "var type for i variable in for loop ".
        Addition to above answer I will suggest below snippet where you can get rid of hard coding for model count in function

        function adjustText(repeaterItem) {
        for (var i = 0; i < repeaterItem.count; i++) {
        if(repeaterItem.itemAt(i).text === ("Hello"+ i))
        {
        repeaterItem.itemAt(i).text = "New Text"
        }
        else{
        repeaterItem.itemAt(i).text = "Hello"+ i
        }

            }
        }
        
        Timer{
            id: timer
            interval: 1000
            running: true
            repeat:true
            onTriggered: {
                root.adjustText(artTextRepeater)
            }
        }
        

        Thanks

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dxmachina
          wrote on 28 Mar 2016, 16:47 last edited by
          #4

          Thanks very much, guys. Of course you are 100% correct. Whoops... even more newbish than I feared!

          T 1 Reply Last reply 28 Mar 2016, 19:47
          1
          • D dxmachina
            28 Mar 2016, 16:47

            Thanks very much, guys. Of course you are 100% correct. Whoops... even more newbish than I feared!

            T Offline
            T Offline
            tarod.net
            wrote on 28 Mar 2016, 19:47 last edited by
            #5

            @dxmachina Well, that's not true ;) Just a little mistake. Happy coding! :D

            "Individually, we are one drop. Together, we are an ocean."

            1 Reply Last reply
            0

            1/5

            25 Mar 2016, 19:59

            • Login

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