11-23-2012 02:47 PM
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...
11-23-2012 02:50 PM
11-23-2012 04:26 PM
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/qdeclarativebasic
note: a qml variant property is immutable i.e you cannot change its content without changing its reference.
11-23-2012 04:46 PM
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! :-)
11-23-2012 04:52 PM
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.
11-23-2012 06:16 PM
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...
11-23-2012 07:31 PM
@Ebscer: what do you mean by qml object?
@Peter: glad that helped you out
wasted half a day figuring out why my data wasn't being updated ![]()
11-23-2012 11:07 PM - edited 11-24-2012 12:45 AM
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.
11-24-2012 03:46 AM
Not sure this exactly fits your purpose but have you looked at QDeclarativeListProperty?
Check out this thread. http://supportforums.blackberry.com/t5/Cascades-De
I'm not 100% sure how it works in the all QML case but it works nicely for accessing lists in QML.