PDA

View Full Version : problem with setting properties in creating dynamic objects



aya_lawliet
19th August 2011, 06:27
Hi,

Can someone help me regarding the creation of dynamic objects in QML?

I followed the example on this link http://doc.qt.nokia.com/4.7-snapshot/qdeclarativedynamicobjects.html cause I wanted to create something similar.

I created the example as is ( Sprite.qml and main.qml ), as well as the javascript file.
var component;
var sprite;

function createSpriteObjects() {
component = Qt.createComponent("Sprite.qml");
if (component.status == Component.Ready)
finishCreation();
else
component.statusChanged.connect(finishCreation);
}

function finishCreation() {
if (component.status == Component.Ready) {
sprite = component.createObject(appWindow, {"x": 100, "y": 100});
if (sprite == null) {
// Error Handling
console.log("Error creating object");
}
} else if (component.status == Component.Error) {
// Error Handling
console.log("Error loading component:", component.errorString());
}
}

Works fine but the setting of properties x and y on line 14 does not work.

Is this a bug, or is there something I'm missing? Thank you for your help!

aya_lawliet
23rd August 2011, 08:51
I did a workaround for this that works for me,



sprite = component.createObject(appWindow);
sprite.x = 100
sprite.y = 100


I hope this will help anyone who got stuck in the same situation.

aya_lawliet
23rd September 2011, 17:52
There is a slight problem I found regarding the workaround I mentioned above.

When you set the properties:
sprite.x = 100
sprite.y = 100
This does not mean that the properties are set upon object creation. These properties are set AFTER the object is created.

Thus, when you add custom property like:
property string mode: "auto" (in the QML file)

and updated the value for this in the script:
sprite.mode = "manual"

Upon object creation, the mode will still have "auto" value. And after the object is created, the script will then update the mode value to "manual". I just found this out while creating an application.

I haven't found a solution for setting the property value upon object creation. But when I found the solution, I will post it here. :)

frankiefrank
25th September 2011, 14:44
Playing with similar implementations, will be happy to know what you learned.