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. Avoiding duplicate `objectName` and `id` in Qml

Avoiding duplicate `objectName` and `id` in Qml

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 3 Posters 582 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.
  • P Offline
    P Offline
    patrickkidd
    wrote on last edited by
    #1

    I am using objectName to access QQuickItems from C++ for integration tests in C++. I am getting tired of assigning objectName to a duplicate string value and thought I would automatically assign objectName based on the value of id.

    Anyone k now how to do this? Or to accomplish what I am looking to do? It seems like such a waste of time to have such duplicate values for larger apps.

    Thanks!

    https://alaskafamilysystems.com/

    1 Reply Last reply
    3
    • C Offline
      C Offline
      Charles Liao
      wrote on last edited by
      #2

      The most efficient operation I found:

      1. click the "@" button like the picture.
        image.png
        This operation will export the item as a alias property of root item in qml, which add the code "property alias connect_button: connect_button" in qml file. The property name is same with id name.
      2. use the following code in cpp that can get the qml item with "id":
        QQuickItem* connect_button = qmlRoot->property("connect_button").value<QQuickItem *>();
      1 Reply Last reply
      0
      • GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #3

        Not as straight forward as using objectName but you could retrieve the id of an object in a context with QQmlContext::nameForObject. The trick is to get the context you want because an object can have different ids in different contexts.

        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