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
Curahee
Posts: 226
Registered: ‎01-12-2013
My Device: z30
Accepted Solution

QML QVariantList to QString converter?

Hey All

 

I'm fairly new to BB programming. I have some experience in Android and Windows Phone but I think BB10 will kick a lof of **bleep**!!

 

I done a little programming for my app and I use a webservice to download the json data from the server. Now I programmed a master-detail view with qml, so when clicking an item in the list, I get the details of that item.

 

But the item has also QVariantLists because it's an array of data. For instance, in my json file I have the property language: ["Dutch",  "English", "French"].

 

Now I want to show these multiple languages in my detailpage. Is it possible to write a converter or something like that? Or should I do the processing before I add the data to my datamodel?

 

Thanks in advance!

______________________________________________________
BB10-OAuth: GitHub
Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: QML QVariantList to QString converter?

QVariant-s work just like javascript arrays in QML.
If you know how to access this QVariantList from QML you can also iterate it appending all members to a string.

var s = "";
for (var i = 0; i < myList.length; i++) {
 console.log(myList[i]);

 if (i != 0)
   s += ", ";
 s += myList[i];
}

 


Andrey Fidrya, @zmeyc on twitter
Developer
Curahee
Posts: 226
Registered: ‎01-12-2013
My Device: z30

Re: QML QVariantList to QString converter?

Very nice! I just added a javascript function, in this way I can reuse it.

 

function arrayToString(array) {
    var result = '';
    
    for(var i=0; i<array.length-1; i++) {
        result += array[i] + ', ';
    }
    
    result += array[array.length-1];
    
    return result;
}

 

But is it slower if you use javascript? Because it's possible to convert it with C++. Or will the compiler convert all this to C++?

______________________________________________________
BB10-OAuth: GitHub
Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: QML QVariantList to QString converter?

[ Edited ]

This line will reference an invalid index for zero length arrays:

result += array[array.length-1];

 

QML is compiled into bytecode. It's a bit slower than native code, but unless you do some heavy computation and see visible performance degradation it's usually not worth optimizing anything.

 

Btw, the most efficient way to concatenate strings in C++ is using QStringBuilder.

http://blog.qt.digia.com/blog/2011/06/13/string-concatenation-with-qstringbuilder/

 

 

In Javascript there's no StringBuilder, but join()/arrays can be used instead. I don't know how much faster it is actually, it probably depends on implementation:

var a = [];
a.push("x");
a.push("y");
return a.join("");

 

Personally I rarely use QML. It can be useful sometimes, but with convenient builders Cascades introduced I find it much simpler to just declare everything in C++ code without doing any bridging between QML & C++.

 


Andrey Fidrya, @zmeyc on twitter
Developer
Curahee
Posts: 226
Registered: ‎01-12-2013
My Device: z30

Re: QML QVariantList to QString converter?

Yes I know, I allready added the if-statement to check if their are more then 0 values.

 

Thanks for reminding me about the .join() in javascript. I knew that function, just forgot about it.

 

Actually I personally prefer to split things up (view and logic). This is also done in Windows Phone & Android. This way it's easier to create another view with the same functionality withouth diving in your code.

______________________________________________________
BB10-OAuth: GitHub