PDA

View Full Version : Convery QByteArray to QVariantMap



karankumar1609
29th May 2013, 07:29
Hello All,,


I have converted the QVariantMap (http://qt-project.org/doc/qt-4.8/qvariant.html)to QByteArray (http://qt-project.org/doc/qt-4.8/qbytearray.html) via:


QByteArray parameters;
foreach( QVariant variant, values )
parameters.append( variant.toChar() );


But i want to convert it back in QVariantMap.

Any idea about converting QByteArray (http://qt-project.org/doc/qt-4.8/qbytearray.html) to QVariantMap (http://qt-project.org/doc/qt-4.8/qvariant.html)

ChrisW67
29th May 2013, 09:32
You can't recover the original QVariantMap... you have thrown away the QString keys from the map, and any QVariant value that was not convertable to QChar, and the original type (char, int or unit) of the QVariants that could be converted to char.

You probably should consider using QDataStream to serialise/deserialise into a buffer.

karankumar1609
29th May 2013, 12:46
Works for me:
I USE


QByteArray parameters;
QDataStream *stream = new QDataStream(&parameters, QIODevice::WriteOnly);

(*stream) << values;
delete stream;
stream = NULL;

ChrisW67
30th May 2013, 21:19
This will only work if you used QDataStream to serialise the QVariantMap in the first place.

There's no need to allocate the QDataStream on the heap at line 2. The syntax at line 4 is simplified, and lines 5 and 6 become unnecessary.