Dyanmic Id problem
-
hi guys
I just came across a problem.
Is it possible to change id of qml on any event like mouseclickexample:
Window { //id:mainwindow visible: true width: 640 height: 480 title: qsTr("Hello World") Rectangle { id:four anchors.centerIn:parent height:100 width:100 color:"green" MouseArea { anchors.fill:parent onClicked:{parent.id="changed";console.log("parent id"+parent.id)} } } }
actually in my main project I am using a qml and creating 4 objects using .js . Now I want unique ids for all 4 objects. Another problem is a single qml object will work for 2 modes so for each mode I want a unique id. how can I achieve this?
-
Hi
From the docsThe id Attribute
Every QML object type has exactly one id attribute. This attribute is provided by the language itself, and cannot be redefined or overridden by any QML object type.A value may be assigned to the id attribute of an object instance to allow that object to be identified and referred to by other objects. This id must begin with a lower-case letter or an underscore, and cannot contain characters other than letters, numbers and underscores.
-
@ashajg said in Dyanmic Id problem:
Is it possible to change id of qml on any event like mouseclick
No. You can only set
id
at edit-time, not at run-time.id
is always static; it is never dynamic.I am using a qml and creating 4 objects using .js . Now I want unique ids for all 4 objects.
Store your objects in JavaScript variables (
var
). You can dynamically access your objects using JavaScript code.Another problem is a single qml object will work for 2 modes so for each mode I want a unique id.
Store a "mode" enum. Change the enum when you change modes.