[Qt5-feedback] C++ api to use for UI in addition to QML

Alan Alpert alan.alpert at nokia.com
Wed May 18 03:02:57 CEST 2011


On Tue, 17 May 2011 23:45:10 ext Kishore wrote:
> On Tuesday 17 May 2011 4:17:03 PM Andre Somers wrote:
> > > You can't find an item by id from C++, it's a detail internal to the
> > > QML engine. ObjectName will continue to work in Qt 5 though.
> > 
> > IMO, you should be able to do just that. I know you are not able to do
> > that right now, but I think that should be fixed. id's are the way you
> > reference objects in QML. You give objects an id, an then do stuff on
> > them. That is hardly an "internal detail". It simply doesn't make sense
> > to ask people to also set the ObjectName property in order to be able to
> > access the same object from C++. It doesn't add to readability either.
> > Why isn't the id simply assigned as the object name by default? "Code
> > less, Create more" remember?
> 
> IIRC id used for an object can only be referenced from within the same qml
> file. Hence id need only be unique within a single qml file.
> 
> IMHO this characteristic of qml makes it inappropriate to use for
> referencing from c++.

Kishore is correct. Ids do not have a one-to-one mapping with objects. Not 
only can multiple objects be given the same id, but the same object can have 
different ids in different scopes. 

Consider the example below, and what the objectName of the Rectangle would be 
in main.qml if id set objectName as well (answer: undefined, but probably 
'instance'). You don't want to start relying on implementation details.

MyType.qml
import QtQuick 1.1

Rectangle{
	id: container
}

main.qml

import QtQuick 1.1
MyType{
	id: instance
}

-- 
Alan Alpert
Senior Engineer
Nokia, Qt Development Frameworks


More information about the Qt5-feedback mailing list