4
  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?
  • 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. – David Foerster Oct 19 '14 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. – Akiva Oct 19 '14 at 20:43
7

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?

| improve this answer | |
2

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)

| improve this answer | |

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.