Qt World Summit: Submit your Presentation

Save QImage to Base64 String

  • Hi Guys!

    I've done some code to create an image and so far it works as expected. I can save it to (png) file. So far so good. But now i'd like to save the same image to a buffer and convert it to a base64 string. I tough this is an easy task but so far no luck.

    QByteArray ba;
    QBuffer bu(&ba);
    image.save(&bu, "PNG");
    QString imgBase64 = ba.toBase64();
    qDebug() << "imgBase64: " << imgBase64;

    The qdebug does not get printed. No errors. I have no clue why it isn't working.

    Any suggestions?


  • Lifetime Qt Champion

    @qDebug Not even "imgBase64: " is printed? You could try to debug and see what imgBase64 contains after assignment.

  • Nope, not even the string before. The whole line gets ignored somehow. If i place a qDebug before and after, both get printed. There is nothing to debug. No errors come up.

    I only gen an error in 5.7.0 (Windows MSVC), when i set "Terminal" as font

    DirectWrite: CreateFontFaceFromHDC() failed (...) for QFontDef(Family="Terminal", pointsize=12, pixelsize=16, styleHint=5, weight=50, stretch=50, hintingPreference=0) LOGFONT("Terminal", lfWidth=6, lfHeight=-16) dpi=96

    But if i use other fonts, the error is gone. And even with this error showing up, the app compiles and runs using terminal as font. That can't be related to by base64 problem, since i use other fonts without errors.

  • Lifetime Qt Champion

    @qDebug You can try to print using std::cout.
    And you always can debug, not only if an error occurs: place a break point at this line:

    qDebug() << "imgBase64: " << imgBase64;

    and check the content of imgBase64.

  • I did set a breakpoint and run the debug, just without any results. As i mentioned before, the only debug error occurs if i use terminal font, the rest of the code shows no sign of problems.

    std::cout << imgBase64.toStdString();

    Shows the base64 string. Strange.

  • Lifetime Qt Champion

    @qDebug said in Save QImage to Base64 String:

    debug error

    Again: it is not about getting the error. If you have the breakpoint then your app should stop there if you run it through the debugger. Then you can inspect the values of your variables like imgBase64 to see whether they have expected values or not.

  • Again, i did that. I did set a break point i did debug. There is nothing to debug, no relevant data shows up. If you don't believe me, i can take screen for ya ;)

    Can we end the talk about debugging please, it is getting boring :D

  • Lifetime Qt Champion


    You don't open your buffer for writing.

    See the example code from the constructor your are using.

  • I did try to open the buffer (and close it).

    QByteArray ba;
    QBuffer bu(&ba);
    image.save(&bu, "PNG");
    //QString imgBase64 = ba.toBase64();
    QString imgBase64 = QString::fromLatin1(ba.toBase64().data());
    qDebug() << "image base64: " << imgBase64;

    qDebug can't show the base64 string for some reason because

    std::cout << imgBase64.toStdString();

    prints the correct base64 string. I did try serval version of code before i posted here. If i read the png from file and get a base64 from it, the same happens without the buffer. qDebug won't print out the base64 string.

    QByteArray ba;
    ba = file2.readAll().toBase64();
    qDebug() << "base64 from png: " << ba.data(); // nothing
    std::cout << ba.data(); // works

    I wonder why.