Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Ebscer
Posts: 875
Registered: ‎08-31-2009
My Device: 9530, 9630, 9800, 8530, 9900, 9810, 9930, PlayBook, Dev Alpha

Array of QML objects

I have been unable to figure out how to create an array of qml objects. I think that the solution most likely involves the use of QVariant, but the qml seems unaware that such a data type even exists...


Read my thoughts on BlackBerry Development at news.ebscer.com
Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: Array of QML objects

For pretty much any non-primitive (and certainly for non-trivial) types I've been resorting to JavaScript in .js files associated with the .qml. I've seen enough weirdness with e.g. the properties not being able to store "null" either alone or in objects, that I'm sticking with that until I understand more about the issues.

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
Developer
lew
Posts: 174
Registered: ‎03-05-2009
My Device: Dev Alpha

Re: Array of QML objects

in qml, you need to use variant and not QVariant:

 

property variant anArray: ["1","2","3"]

 

see below for documentation on supported built-in qml types:

http://qt-project.org/doc/qt-4.8/qdeclarativebasictypes.html

 

note: a qml variant property is immutable i.e you cannot change its content without changing its reference. 

Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: Array of QML objects


lew wrote:

note: a qml variant property is immutable i.e you cannot change its content without changing its reference. 


Interesting... that little nugget may explain most or all of the difficulties I'd been having with them! :-)


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: Array of QML objects

The page for "variant" under lew's linked page above on all the types is http://qt-project.org/doc/qt-4.8/qml-variant.html

It has some very very useful things to know, and I wish I'd thought to read it through before. I didn't because I was just making assumptions about "variant", but it acts quite differently than I originally expected.

As it turns out, I'd made some of the right guesses of what to do after investigating the problems however, including realizing that copying the entire structure to change one item was likely to be very inefficient.  That's when I switched to using JavaScript for non-trivial structures.

Quoting: One way to "update" the contents of an array or map is to copy the property to a JavaScript object, modify the copy as desired, and then reassign the property to the updated copy. Note, however, that this is not efficient. In the example below, which reassigns the attributes property, the entire set of key-value pairs must be serialized and deserialized every time it is copied between a JavaScript object and a QML property [example remove].  Since this operation is inefficient, if a list or map should be modifiable, it is better to use alternative approaches. For example, you could implement a custom C++ list element, or write to a JavaScript object defined from within a JavaScript file.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
Ebscer
Posts: 875
Registered: ‎08-31-2009
My Device: 9530, 9630, 9800, 8530, 9900, 9810, 9930, PlayBook, Dev Alpha

Re: Array of QML objects

It does not appear as if variant is capable of holding any QML objects. Although if I am unable to edit them, it is a bit besides the point...


Read my thoughts on BlackBerry Development at news.ebscer.com
Please use plain text.
Developer
Developer
lew
Posts: 174
Registered: ‎03-05-2009
My Device: Dev Alpha

Re: Array of QML objects

@Ebscer: what do you mean by qml object?

 

@Peter: glad that helped you out :smileyhappy: wasted half a day figuring out why my data wasn't being updated :smileysad:

Please use plain text.
Developer
Ebscer
Posts: 875
Registered: ‎08-31-2009
My Device: 9530, 9630, 9800, 8530, 9900, 9810, 9930, PlayBook, Dev Alpha

Re: Array of QML objects

[ Edited ]

lew wrote:

@Ebscer: what do you mean by qml object?

 


It is possible that my terminology is wrong. By object I mean a custom .qml item that I have created. For example I have created Dice.qml (code below) and I would like to have a whole array of these objects so that it is easier to itterate through them.

 

Dice.qml

import bb.cascades 1.0

Container
{
    property int value: 0
    property alias width:dice.preferredWidth
    property alias height:dice.preferredHeight
    
    leftPadding: 5
    rightPadding:5
    
    function setSize(size)
    {
        width=size
        height=size
    }
    
    ImageView
    {
        id: dice
        imageSource:
        {
            setImage()
        }
        function setImage()
        {
            imageSource = "asset:///images/"+value+".png"
        }
    }
    
    onValueChanged:
    {
        dice.setImage()
    }
}

 

 After looking through the samples the Kakel comes somewhat close to what I want to do, but does it completly within c++. However, I am not sure if I want to switch languages everytime I want to loop through something, givent that my maximum array size is only five it may be easier to just manually repeat all of the code.


Read my thoughts on BlackBerry Development at news.ebscer.com
Please use plain text.
Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900

Re: Array of QML objects

Not sure this exactly fits your purpose but have you looked at QDeclarativeListProperty? 

 

Check out this thread. http://supportforums.blackberry.com/t5/Cascades-Development/Access-QList-from-QML/m-p/1986913#M6235

 

I'm not 100% sure how it works in the all QML case but it works nicely for accessing lists in QML.

Like all of my posts
Please use plain text.