Question

I'm using QJson to parse data returned in json format. One of the returned items is a 80 character byte array. The return from QJson is a QVariantMap that appears to be an array of long integers when viewed in the Qt Creator debugger.

Is there a better way to convert the QVariantMap to a Byte array than iterating over the Map, casting each QVariant to a byte and assigning it to the byte array?

QByteArray byteArray[60];
QVariantMap returnedMap;
for (int n=0; n< returnedMap.count(); ++n){
  byteArray[n] = (char)returnedMap[n];
}

(Code above is for illustration, I'm not certain what the proper syntax is to cast an integer to a byte. The code above results in 'invalid cast from type 'QVariant' to type 'char')

Was it helpful?

Solution

I don't know why would you aver want to cast a map, which is a numer of "key-value" pairs into an array of values, but if you want to, the syntax would be something like

QByteArray arr;
QVariantMap map;
foreach( QVariant tmp, map )
    arr.append( tmp.toChar() );

Note that while iterating through the map, you go fro key to key in ascending order, so the first element of the array would be such with the lowest key, and vice versa.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top