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. Dyanmic Id problem
QtWS25 Last Chance

Dyanmic Id problem

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 4 Posters 858 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.
  • ashajgA Offline
    ashajgA Offline
    ashajg
    wrote on last edited by
    #1

    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?

    JKSHJ 1 Reply Last reply
    0
    • G Offline
      G Offline
      GrahamLa
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      1
      • ashajgA Offline
        ashajgA Offline
        ashajg
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Astrinus
          wrote on last edited by Astrinus
          #4

          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.

          1 Reply Last reply
          1
          • ashajgA ashajg

            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?

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

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

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            2
            • ashajgA Offline
              ashajgA Offline
              ashajg
              wrote on last edited by
              #6

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

              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