[Qt-qml] component.createObject has gained a mandatory argument
Alan Alpert
alan.alpert at nokia.com
Wed May 5 21:07:54 CEST 2010
Hi,
Another minor change to QML that just occurred is that the createObject
function on Component now requires the parent of the new item to be passed in
as an argument.
For graphical objects (the common case) a common mistake is to not parent a
dynamically created item. Since you almost always want to add a parent, and
it's hard for a beginner to realize what they've done wrong when they forget,
a parent is now a required argument. The function will also set the parent on
the created object. Note that because the argument is required, an error is
thrown if you try to call createObject without arguments like old code did.
All the cases we had set the parent on the next line, so the change that you'd
probably need to get your code working again would be as in the example below.
var a = component.createObject();
a.parent = parentItem;
becomes
var a = component.createObject(parentItem);
If you do not want to set the parent there you can pass null into the
function, but at least you've made that choice explictly.
--
Alan Alpert
Software Engineer
Nokia, Qt Development Frameworks
More information about the Qt-qml
mailing list