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. Change property of Dynamic Item in grid
Forum Updated to NodeBB v4.3 + New Features

Change property of Dynamic Item in grid

Scheduled Pinned Locked Moved Solved QML and Qt Quick
14 Posts 3 Posters 1.6k Views 1 Watching
  • 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.
  • dheerendraD Offline
    dheerendraD Offline
    dheerendra
    Qt Champions 2022
    wrote on last edited by
    #4

    Use the children property of item. e.g

    for(var i=0;i<grid.children.length;i++){
          grid.chidren[i].color="blue"
    }
    

    MouseArea not required for every item of grid.

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

    1 Reply Last reply
    2
    • E Offline
      E Offline
      echo55
      wrote on last edited by
      #5

      Alright great news,

      I want it to be trigger when i click on of the Items, but it needs to access grid which isn't in the same file .
      If i do a function and try to call it in the children it doesn't work (not recognized).

      Here is an easy way to do that ?

      Thank you.

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #6

        What do you mean by not in same file ? Can you give me the sample code please ?

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

        1 Reply Last reply
        1
        • E Offline
          E Offline
          echo55
          wrote on last edited by
          #7

          the two code i sent just up .

          It's from two different files (.qml)
          i use

          Qt.createComponent("Bundle_content.qml")
          

          to create in the current file a component from another file so Bundle_content.qml

          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by
            #8

            Objects created from Button_Component.qml becomes child of Grid. Why do you want to access the parent grid inside the child objects ? Do you want to change something in grid ?

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

            1 Reply Last reply
            1
            • E Offline
              E Offline
              echo55
              wrote on last edited by
              #9

              I want to change the variable color gradcolor1 and gradcolor2 from the grid .
              but only when the mousearea of any of the children of grid is pressed.

              1 Reply Last reply
              0
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Qt Champions 2022
                wrote on last edited by
                #10

                Define the signal in your component. Emit the signal when you click. Catch the signal in main. Not a good idea to access the parent inside the child objects.

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

                1 Reply Last reply
                2
                • E Offline
                  E Offline
                  echo55
                  wrote on last edited by
                  #11

                  I tried to do that but the problem is that i want to change color of every component except the one clicked .
                  How can i send a signal saying to change color of all of it minus the one who emitted it .

                  1 Reply Last reply
                  0
                  • dheerendraD Offline
                    dheerendraD Offline
                    dheerendra
                    Qt Champions 2022
                    wrote on last edited by dheerendra
                    #12

                    OK. You can try something similar to the following. It is simple concept to show how you can work with object.

                    ===== main.qml =========
                    import QtQuick 2.11
                    import QtQuick.Window 2.11
                    
                    Window {
                        visible: true
                        width: 440
                        height: 480
                        title: qsTr("Hello World")
                        property var comp:null
                    
                        function changeColor(val){
                            for(var i=0;i<grid.children.length;i++){
                                var obj = grid.children[i];
                                if (obj.val===val){
                                    continue;
                                }else {
                                    obj.color = "blue"
                                }
                            }
                        }
                    
                        Grid {
                            id : grid
                            spacing: 5
                            anchors.fill: parent
                            Component.onCompleted: {
                                for (var i=0;i<12;i++){
                                    if (comp==null){
                                        comp = Qt.createComponent("ChildElement.qml");
                                    }
                                    var obj = comp.createObject(grid)
                                    obj.changeEveryBody.connect(changeColor);
                                    obj.val = i;
                                }
                            }
                        }
                    }
                    ========= ChildElement.qml ===============
                    import QtQuick 2.0
                    
                    Rectangle {
                        width: 100;height: 100;color: "red"
                        property int val:0
                        signal changeEveryBody(int val);
                        MouseArea{
                            anchors.fill: parent
                            onClicked: {
                                changeEveryBody(val)
                            }
                        }
                    }
                    

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

                    1 Reply Last reply
                    3
                    • E Offline
                      E Offline
                      echo55
                      wrote on last edited by
                      #13

                      Thank you very much.
                      It worked.

                      1 Reply Last reply
                      0
                      • dheerendraD Offline
                        dheerendraD Offline
                        dheerendra
                        Qt Champions 2022
                        wrote on last edited by
                        #14

                        cool. please move the case to solved state

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

                        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