12

I have a string id of an object I need to find in QML tree. For example:

var idToFind = "myBtnId"

Can I do something like the following?

var objectThatINeed = myMainWindow.findObjectById(idToFind)

As far as I understand I can use objectName for this purpose (at least from C++). Can I still reuse existing ids somehow without introducing the names?

I want to use this object as a parent for some other dynamically created controls.

BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
Werolik
  • 923
  • 1
  • 9
  • 23
  • 1
    You cannot refer to object by its `id` from C++. It's not as regular property, you cannot change it and `id` exists only in QML scope. But you easy can use `objectName` instead of `id` as @Miki noticed. – folibis Jul 04 '15 at 02:22

4 Answers4

9

No, you have to use objectName or some other property.

The id Attribute:

Once an object instance is created, the value of its id attribute cannot be changed. While it may look like an ordinary property, the id attribute is not an ordinary property attribute, and special semantics apply to it; for example, it is not possible to access myTextInput.id in the above example.

Mitch
  • 23,716
  • 9
  • 83
  • 122
  • Indeed, id isn't an ordinary property, but that doesn't mean you can't access it. The id is exposed via the QQmlContext, using the `nameForObject()` method. – André Jan 13 '23 at 08:53
  • True, I don't think I was aware of that when I answered this. I wouldn't recommend it or findChild() though. The latter can be useful for testing, but in general, C++ should only expose API to QML, not the other way around: https://doc.qt.io/qt-6/qtquick-bestpractices.html#exposing-data-from-c-to-qml – Mitch Jan 14 '23 at 14:38
  • Yeah, I concur with that. I mainly use it for providing error reporting in custom QML components (if qmlWarning or such doesn't suffice), but I did want to point it it is possible. – André Jan 16 '23 at 05:28
5

If you know the IDs of all items you want beforehand, you can create an object map for them and use that to look them up. For example:

Item {
    property var idMap: ({ foo:foo, bar:bar })
    Rectangle { id:foo }
    Item { id:bar }

    function findItemById(id) {
      return idMap[id];
    }

    Component.onCompleted: console.log(findItemById('foo'), findItemById('bar'))
    // qml: QQuickRectangle(0x1479e40) QQuickItem(0x148fbf0)
}
Phrogz
  • 296,393
  • 112
  • 651
  • 745
1
QJSValue MyEngine::getQmlObjectById(const QString& id) {
    QV4::ExecutionEngine *v4 = QV8Engine::getV4(m_JSEngine);
    QV4::Scope scope(const_cast<QV4::ExecutionEngine *>(v4));
    QV4::ScopedString name(scope, v4->newString(id));
    return QJSValue(v4, v4->currentContext->getProperty(name));
}
kaegoorn48
  • 129
  • 1
  • 2
1

Based on the answer from Phrogz, if you don't want an explicit map, you can assign the components to properties and then reference these with the [] operator:

Item {
    id: page
    property Component foo: Rectangle { color: "yellow" }
    property Component bar: Item {  }

    Component.onCompleted: console.log(page["foo"], page["bar"])
    //qml: QQuickRectangle(0x...) QQuickItem(0x...)

} 
Amfasis
  • 3,932
  • 2
  • 18
  • 27