Solved Change property of Dynamic Item in grid
-
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.
-
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.
-
What do you mean by not in same file ? Can you give me the sample code please ?
-
the two code i sent just up .
It's from two different files (.qml)
i useQt.createComponent("Bundle_content.qml")
to create in the current file a component from another file so Bundle_content.qml
-
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 ?
-
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. -
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.
-
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 . -
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) } } }
-
Thank you very much.
It worked. -
cool. please move the case to solved state