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
gdev001
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator
Accepted Solution

Traverse QVariantList

[ Edited ]

Hi,

I have some JSON data which I import into a QVariantList, and using code below

I am trying to traverse this list and print out the items. But I have two issues:

 

   JsonDataAccess jda;
   QString path;
   path = QDir::currentPath() +
         "/app/native/assets/employees.json";
   QVariant list = jda.load(path);

   QVariantList temp = list.value<QVariantList>();
   for (int i = 0; i < temp.size(); ++i) {
       QString gStr = temp[i].toString();
       qDebug()<<"String Value: "<<gStr;
   }

 1. How to use qDebug properly in Momentics IDE? OS: Mac OS X; nothing gets printed even if I remove the gStr variable from that line

2. Is it correct way to traverse the QVariantList and print contents of its elements?

 

ps. the temp is correctly initialized I manage to fill a ListView using it later on.

 

Developer
Zmey
Posts: 1,513
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: Traverse QVariantList

Hi,

1. Please check "Known limitations" section in SDK release notes, it contains information on setting up the logs:

https://developer.blackberry.com/cascades/download/releasenotes/

 

2. The code is correct. Another way to iterate the list:

 

foreach (QVariant v, temp)
{
  qDebug() << "String Value: " << v.toString() << "\n";
}

 

You might also want to add newline to flush the output otherwise last line can be lost on application crash.

 

Also foreach makes a copy of list before iterating. Qt performs lazy copying of data (copy-on-write), so this isn't very expensive and you can safely modify the container during iteration.


Andrey Fidrya, @zmeyc on twitter
Developer
gdev001
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Traverse QVariantList

[ Edited ]

Yep, thanks. just one brief question. In my case it appears my "temp" is an array of 3 objects. Where clearly each object is a QVariantMap. Now I want to print out the contents of some keys (say "key1") in these QVariantMaps objects. do you have some code snippet that would do this? ps. I actually have issues converting the QVariantList item to a QVariantMap; I know how to print contents of a QVariantMap

Developer
Zmey
Posts: 1,513
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: Traverse QVariantList

QVariantMap m = v.value<QVariantMap>();
qDebug() << m["key"].toString();

 


Andrey Fidrya, @zmeyc on twitter
Developer
gdev001
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Traverse QVariantList

[ Edited ]

Yes I was about to write I had found a similar solution; but now my debug logging is not working.. a anyway thanks I will try to find smth.