Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Dyanmic Id problem



  • hi guys

    I just came across a problem.
    Is it possible to change id of qml on any event like mouseclick

    example:

    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 docs

    The 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.



  • Hi @GrahamLa

    Thanks for reply

    What this line exactly means 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



  • That, if you do not assign a value to an object id, you cannot refer to the object. But it can be assigned only once.


  • Moderators

    @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.



  • @GrahamLa @JKSH @Astrinus thank you guys!!!


Log in to reply