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
Super Contributor
Posts: 398
Registered: ‎01-12-2011
My Device: Q10
My Carrier: Fido
Accepted Solution

Extract a string from QVariantList.

Hi,

 

I'm using SQL in my application and have looked at the Quotes example, which queries the quotes table and stores the response in a QVariantList, but I can't figure out how to extract the string value of a single field, I've found the following post:

 

http://supportforums.blackberry.com/t5/Cascades-Development/How-to-extract-value-from-QVariantlist/m...

 

Where the answer apparently worked, but when I try it I get the following error:

 

error: expected primary-expression before '>' token

 

How do I get values from a QVariantList?

 

Thanks!

 

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Extract a string from QVariantList.

Hi,

 

Please post your current code & full compiler error message.

 

If you have QVariant initially, QVariantList can be extracted using this command:

QVariantList lst = data.toList();

QVariant is just a wrapper around other data types. They can be extracted using toMap(), toList(), toString() etc.

.value<typename>() does the same, it's just another syntax.

 

Then iterate the entries:

foreach (QVariant entry, lst)
{
  QVariantMap m = entry.toMap();
  QString str = m["fieldName"].toString();
  // do something with str
}

Or, if you have a single field:

QString val;
if (!lst.isEmpty()) // important: if the list is empty, calling first() will crash the app
{
  val = lst.first().toString();
}

 

Before parsing the data, print it using qDebug() to see which format it has:

qDebug() << data;

 

 I hope this will help.

 

 


Andrey Fidrya, @zmeyc on twitter
Highlighted
Super Contributor
Posts: 398
Registered: ‎01-12-2011
My Device: Q10
My Carrier: Fido

Re: Extract a string from QVariantList.

Thanks! I used the syntax you provided in your answer and all is well. The only error message I got was the one I posted. It seems that my syntax was fine as similar syntax is used elsewhere in my code.