PDA

View Full Version : How to load base64 image data from json in QT



phpkode
9th January 2015, 06:53
I am beginning with QT5 and trying to load an image from base64 json data. I can load directly from a base64 string but is unsuccessful when trying to load from json object.

the error i am getting is error: conversion from 'QJsonValueRef' to non-scalar type 'QByteArray' requested

I tried changing toUtf8 toAcsii() etc. but similar error are being produced. Any help and suggestions will be much appreciated.



QString strReply = (QString)reply->readAll(); // json data from a servlet (created using gson library)
QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
QJsonObject jsonObj = jsonResponse.object();
QByteArray imgbase64 = jsonObj["photo"]; // image data
QImage img;
img.loadFromData(QByteArray::fromBase64(imgbase64) );
ui->outputImage->setPixmap(QPixmap::fromImage(img));
ui->outputImage->setScaledContents( true );
ui->outputImage->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored );

wysota
9th January 2015, 07:10
The operator[] returns a QJsonValue which you need to convert to string using QJsonValue::toString() and then you can continue converting to a byte array.

anda_skoa
9th January 2015, 09:36
And you needlessly perform a full roundtrip conversion of text between 8-bit and 16-bit representation in the first two lines.

Cheers,
_

phpkode
9th January 2015, 10:49
The operator[] returns a QJsonValue which you need to convert to string using QJsonValue::toString() and then you can continue converting to a byte array.

I am not able to understand it completely
i tried few things like this



QJsonValue qjv = jsonObj["photo"];
QString qs = QJsonValue::toString(qjv);
QByteArray qba;
qba.append(qs);


now I am getting error: no matching function for call to 'QJsonValue::toString(QJsonValue&)'
QString qs = QJsonValue::toString(qjv);

wysota
9th January 2015, 11:10
toString() is not a static method.