[Qt-qml] Getting the id

Alan Alpert alan.alpert at nokia.com
Fri Nov 5 08:17:49 CET 2010


On Fri, 5 Nov 2010 16:59:00 Lahtela Tuukka.M (EXT-Tieto/Tampere) wrote:
> Hi!
> 
> This is somewhat related to the “Find by id” thread. Is it somehow possible
> to get id of the of a qml element. It would be useful for our automated
> testing tool.
> 
> Rectangle {
>     id: myRect
>     objectName: "theRectangle"
> }
> 
> So basically I would need to get the id of the element in c++ code (for
> example  the myRect value in the example qml).  I do not seem to find a
> way to do this.
> 
> 
>  *   Tuukka Lahtela

It is not possible.

id only exists in QML and it does not map cleanly to C++ instances. Consider 
the following example:

Item{
	id: a
	Repeater{
		id: repeater
		model: 5
		delegate: Item{
			id: delRoot
			Text{
				id: delText
				text: index; anchors.fill: delRoot;
			}
		}
	}
}

In the QML file there are four elements with their own ids. When instantiated, 
you have eight items, including five instances of the item described by 
delRoot.

There are also cases where one C++ instance may have multiple ids through 
being used in multiple files.

For testing purposes, consider setting the objectName property and using that. 
Also be aware of the existing automated testing options that Michael mentioned 
in the find by id thread. As those test frameworks work inside QML, I believe 
that you can access elements by id in your tests.

-- 
Alan Alpert
Software Engineer
Nokia, Qt Development Frameworks




More information about the Qt-qml mailing list