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
Highlighted
Developer
Posts: 19
Registered: ‎03-06-2014
My Device: blackberry z10 stl100-3
My Carrier: n/a
Accepted Solution

Working with QVariantMap

Posted this question 2 weeks ago here and on the QT forums and got no replies Smiley Sad

I'm desperate and have made no inroads on what seems to be a relatively simple problem.

I've managed to simplify a QVariantMap to this:

 

QMap(
    ("_MoreStuff", QVariant
	(QVariantList, 
	    (QVariant
		(QVariantMap, QMap
		     (("stuffID", QVariant(QString, "1") ) 
		     ( "stuffValue" ,  QVariant(QString, "I want this value 1") ) )
				) ,   
	     QVariant
		  (QVariantMap, QMap
		      (("stuffID", QVariant(QString, "2") ) 
		       ( "stuffValue" ,  QVariant(QString, "I want this value 2") ) )
				) ,  
	      QVariant(
		   QVariantMap, QMap
			(("stuffID", QVariant(QString, "3") ) 
			( "stuffValue" ,  QVariant(QString, "I want this value 3") ) )  
				) ...

 I need to loop through this list and extract the stuffID's and StuffValue's

 

I've tried a bunch of different loops like this:

 

QVariant stuff;
for(QVariantMap::const_iterator iter = datamap.begin(); iter != datamap.end(); ++iter) {
     qDebug() << "Keys Are: " << iter.key() << "Values Are: " << iter.value();
      stuff = iter.value();
}
foreach ( QVariant item , stuff.toList())
{
       qDebug() << "listing stuff items: ";
       qDebug()<<item.type();
}

 

and the best I get is a loop through the entire thing but only get empty quotations "" or nothing at all.

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Working with QVariantMap

The examples you are giving only access the top QVARIANTMAP, there are several levels of in your data and all need to be converted and looped through.

 

Probably the reason that you haven't got an answer is that no one wants to spent the time replicating your data and then writing all the code.

 

Why don't you start with a simpler structure with maybe a list within a map and get that working first so you understand how these relate to each other and how to access the elements?


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 19
Registered: ‎03-06-2014
My Device: blackberry z10 stl100-3
My Carrier: n/a

Re: Working with QVariantMap

Literally figured it out 5 minutes after posting this, lol.

QVariant stuff;
    			for(QVariantMap::const_iterator iter = datamap.begin(); iter != datamap.end(); ++iter) {
    			  qDebug() << "Keys Are: " << iter.key() << "Values Are: " << iter.value();
    			  stuff = iter.value();
    			   //datalist = stuff.toMap();
    			}
    			foreach ( QVariant item , stuff.toList())
    			{
    				//qDebug() << "listing stuff items: ";
    				QVariantMap mapstuff = item.toMap();
    				int i = 0;
    				QString itemkey = "";
    				QString itemValue = "";
    				for(QVariantMap::const_iterator iter = mapstuff.begin(); iter != mapstuff.end(); ++iter) {
    				    //qDebug() << "Keys Are: " << iter.key() << "Values Are: " << iter.value();
    				    if (i == 0){
    				    	itemkey = iter.value().toString();
    				    }else{
    				    	itemValue = iter.value().toString();
    				    }
    				    i++;
    				}
    				qDebug() << "final key/values: " << itemkey << "->" << itemValue;

    			}

 

 

 this is spitting out the QVariant as a list, then going through each item in the list as a QVariantMap

my output looks like this:

 

final key/values:  "1" -> "I want this value 1"

final key/values:  "2" -> "I want this value 2"

final key/values:  "3" -> "I want this value 3"

 

I'm sure there's a simpler way to do this, but I'm just happy this works.