Avoiding duplicate `objectName` and `id` in Qml
Unsolved
QML and Qt Quick
-
I am using
objectName
to access QQuickItems from C++ for integration tests in C++. I am getting tired of assigningobjectName
to a duplicate string value and thought I would automatically assignobjectName
based on the value ofid
.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!
-
The most efficient operation I found:
- click the "@" button like the picture.
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. - use the following code in cpp that can get the qml item with "id":
QQuickItem* connect_button = qmlRoot->property("connect_button").value<QQuickItem *>();
- click the "@" button like the picture.
-
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.