[Qt-qml] How to add/remove/count items in a list<Item> property from within javascript?

michael.brasser at nokia.com michael.brasser at nokia.com
Tue Oct 26 03:05:17 CEST 2010


On 26/10/2010, at 6:04 AM, ext Bart Kelsey wrote:

> Greets!
> 
> I'm trying to create a list of Items, and it's not clear to me, from within JavaScript, how to add, remove, and count those items.  The function below doesn't seem to work (textArray is declared as "property list<Item> textArray").  Note that my startUp function is being correctly called, and I can verify that the TalkBoxTextComponent.createObject method is working.  I've tried using .push(), .append(), length, etc, and I'm having no luck so far.
> 
> ******
> 
> var TalkBoxTextComponent;
> 
> function startUp() {
>   TalkBoxTextComponent = Qt.createComponent("TalkBoxText.qml");
> }
> 
> function appendText(string) {
>   var object = TalkBoxTextComponent.createObject(talkbox);
>   object.text = string;
>   talkbox.textArray[0] = object;
>   talkbox.textArray[1] = object;
> }

Manipulating QML list properties from javascript is not well documented at the moment (http://bugreports.qt.nokia.com/browse/QTBUG-14645), and as they behave differently from javascript Arrays, it can sometimes get a bit confusing.

The best way I know how to explain the current list property support is to say that writing to a list _property_ is supported, but writing to the list itself is not. To give examples:

(1) textArray = [object, object]    //supported, writes a new array of two items to the property textArray
(2) textArray = []    //supported, writes an empty array to the property textArray (i.e. clears the textArray property)

(3) textArray[1] = object    //not supported, tries to modify the list itself
(4) textArray.push(object)    //not supported, tries to modify the list itself

There are challenges with supporting (3) and (4) and having bindings involving textArray update correctly, which is why they are not yet supported. It's a limitation we don't like, and want to fix in a future release (probably by having lists support some form change notifications, much like list models do). The workaround for now, if you need a writable list, is to use a javascript Array (the samegame demo shows an example of this).

Regards,
Michael



More information about the Qt-qml mailing list