1. What is the objectName: property in QML?
  2. Which components have this property?
  3. Why is it needed?
  4. How is it different than the id: property?
  5. Why can't I just use the id: property instead?
  • 1
    While this is technically on topic on AU, this type of programming question is generally much better off on Stackoverflow. Compare qml tag on AU and qml tag on SO. Oct 19, 2014 at 17:00
  • 1
    I like having all the qml related questions on AskUbuntu, as the individuals who answer are much more familiar with the usecase that will be applicable to me. Also I like the way AU looks better, as well as the community behind it.
    – Anon
    Oct 19, 2014 at 20:43

2 Answers 2


This property holds the name of the object. The objectName is a QString property of QObject which defaults to an empty QString.

You usually set this property when you need to find QML elements from Qt. By this way the signals, properties and functions of a QML element can be accessed from Qt (C++) code.

Autopilot tests use this property to introspect your application.

From Interacting with QML Objects from C++:

QML components are essentially object trees with children that have siblings and their own children. Child objects of QML components can be located using the QObject::objectName property with QObject::findChild(). For example, if the root item in MyItem.qml had a child Rectangle item:

import QtQuick 2.0

Item {
    width: 100; height: 100

    Rectangle {
        anchors.fill: parent
        objectName: "rect"

The child could be located like this:

QObject *rect = object->findChild<QObject*>("rect");
if (rect)
    rect->setProperty("color", "red");

Note that an object may have multiple children with the same objectName. For example, ListView creates multiple instances of its delegate, so if its delegate is declared with a particular objectName, the ListView will have multiple children with the same objectName. In this case, QObject::findChildren() can be used to find all children with a matching objectName.

Warning: While it is possible to use C++ to access and manipulate QML objects deep into the object tree, we recommend that you do not take this approach outside of application testing and prototyping. One strength of QML and C++ integration is the ability to implement the QML user interface separately from the C++ logic and dataset backend, and this strategy breaks if the C++ side reaches deep into the QML components to manipulate them directly. This would make it difficult to, for example, swap a QML view component for another view, if the new component was missing a required objectName. It is better for the C++ implementation to know as little as possible about the QML user interface implementation and the composition of the QML object tree.

If an objectName is needed for testing, this is definitely worth it. objectName’s should obviously not be added when not needed. When adding them, the general QML guidelines for performance should be followed.

Source: What is the impact on memory of adding objectNames to QML items?


1: It's used to lookup objects by name

2: All of them, it comes from QObject

3: It's used by autopilot to introspect your app from test cases

4: I'm not really sure

5: I don't think the id is discoverable from outside tools (like autopilot)

  • More info: 3. Your code can log the objectName for debugging. 4. The "id" is not a normal property: it is like an object pointer. You can pass the id to C++ and you will have a pointer to the QML object. 5. Id is a pointer. From C++ you can navigate the object hierarchy and pass a pointer back to QML and use it like an id: { var parentObj = CppUtil.findParent(root); console.log(parentObj.objectName); }. Jan 27, 2021 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.