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. Problems with accessing individual Repeater Items and with MouseArea
QtWS25 Last Chance

Problems with accessing individual Repeater Items and with MouseArea

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 1.6k 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.
  • M Offline
    M Offline
    MilloMille
    wrote on last edited by
    #1

    Hi,
    I used the Repeater to create a rather large number of identical clickable circles. In addition, there are three Buttons: Select All, Select None & Done.

    If you click on a click on a circle the color should turn form transparent to grey. If you click it again the color should change again. This is my first problem: Once the circle changed the color the first time, there is no changing possible anymore.

    My second problem is to access the Repeater items. I know you can access certain items via itemAt(). But is there also a option to access all items at once. I would need this to set the color of all items at once and also to send the property color of every single items to my .cpp file.

    Here is my code (reduced to the very essentials of the problem)

    property color wellcolor: "transparent"           // with the "Select All" Button i set this property to grey
    
    Repeater {
                                model: 48
                                Rectangle {
                                    id: smallcircle
                                    color: wellcolor
                                    MouseArea {
                                        id:wellselected
                                        anchors.fill:parent
                                        onClicked: smallcircle.color= "darkgrey"            // Here I change the actual color and not the property wellcolor. I know that this is bad coding but i found no other solution
                                    }
                                }
                            }
    

    And here the Button "Select all"

    Rectangle {                        // Push Button: All wells selected
                Text {
                    anchors.centerIn: parent
                    text: "All"
                }
    
                MouseArea {
                    id:allsmall
                    anchors.fill:parent
                    onClicked: wellcolor = "darkgrey"
                    }
    
    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      For first problem inside the clicked signal check existing colour and then change accordingly.
      If small circle.color equals gray change to new color els to gray. Use qt.colorequal function to check it. To access all the children's use children function. See item documentation for this

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      M 1 Reply Last reply
      7
      • dheerendraD dheerendra

        For first problem inside the clicked signal check existing colour and then change accordingly.
        If small circle.color equals gray change to new color els to gray. Use qt.colorequal function to check it. To access all the children's use children function. See item documentation for this

        M Offline
        M Offline
        MilloMille
        wrote on last edited by MilloMille
        #3

        @dheerendra said in Problems with accessing individual Repeater Items and with MouseArea:

        For first problem inside the clicked signal check existing colour and then change accordingly.
        If small circle.color equals gray change to new color els to gray. Use qt.colorequal function to check it.

        Thís worked very well thank you!

        @dheerendra said in Problems with accessing individual Repeater Items and with MouseArea:

        To access all the children's use children function. See item documentation for this

        I tried this before and i tried it again but I don't understand how this works. If i type

        console.log(smallrep.children)   
        

        whereas smallrep is the id of the Repeater, I get [object Object]

        smallrep.children.color
        

        returns undefined.
        If I add an index after children, I get Connot read porperty 'color' of undefinied
        According to the Item documentation the children function returns a list of Items/QObjects. But how can I change/read the color proterty of items with the children function?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MilloMille
          wrote on last edited by
          #4

          Ok i finally got it work, although i used a different approach since i couldnt get the children function to work. I think the code should be fine also:

          Repeater {
                   model: 48
                   Rectangle {
                           id: smallcircle
                           MouseArea {
                                   id:wellselected
                                   anchors.fill:parent
                                   onClicked: Qt.colorEqual("tansparent", (smallcircle.color)) ? smallcircle.color="darkgrey" : smallcircle.color="transparent" 
                                          }
                                      }
                                  }
          
          

          Thanks again for the tip with the qt.colorEqual-function, I liek this solution very much.
          And for the "select all wells"-button

          Rectangle {                        // Push Button: All wells selected
                      Text {
                          anchors.centerIn: parent
                          text: "All"
                      }
                      MouseArea {
                          id:allsmall
                          anchors.fill:parent
                          onClicked: {
                                    for (var i=0; i<48; i++)
                                             smallrep.itemAt(i).color = "darkgrey"
                          }
          }
          

          This also works very well, but it still kinda bothers me i couldnt get it to work with the children function

          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