Results 1 to 5 of 5

Thread: How to load base64 image data from json in QT

  1. #1
    Join Date
    Jan 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default How to load base64 image data from json in QT

    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.

    Qt Code:
    1. QString strReply = (QString)reply->readAll(); // json data from a servlet (created using gson library)
    2. QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
    3. QJsonObject jsonObj = jsonResponse.object();
    4. QByteArray imgbase64 = jsonObj["photo"]; // image data
    5. QImage img;
    6. img.loadFromData(QByteArray::fromBase64(imgbase64));
    7. ui->outputImage->setPixmap(QPixmap::fromImage(img));
    8. ui->outputImage->setScaledContents( true );
    9. ui->outputImage->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored );
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to load base64 image data from json in QT

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to load base64 image data from json in QT

    And you needlessly perform a full roundtrip conversion of text between 8-bit and 16-bit representation in the first two lines.

    Cheers,
    _

  4. #4
    Join Date
    Jan 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to load base64 image data from json in QT

    Quote Originally Posted by wysota View Post
    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

    Qt Code:
    1. QJsonValue qjv = jsonObj["photo"];
    2. QString qs = QJsonValue::toString(qjv);
    3. qba.append(qs);
    To copy to clipboard, switch view to plain text mode 

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

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to load base64 image data from json in QT

    toString() is not a static method.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 0
    Last Post: 12th June 2014, 13:37
  2. Replies: 2
    Last Post: 20th May 2014, 14:53
  3. Replies: 2
    Last Post: 27th March 2013, 05:21
  4. model/view json data displaying
    By binaural in forum Qt Programming
    Replies: 5
    Last Post: 27th June 2012, 09:13
  5. Send Base64 encoded data
    By rmagro in forum Qt Programming
    Replies: 6
    Last Post: 29th October 2007, 17:58

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.