Convert a QIcon (or just a QImage) to base64 and QString

I found difficult to find somewhere a method to convert a QIcon object to a QString to be displayed in base64. I needed it for implementing a nice feature for QuickView mode in Arora. I wanted to display in the HTML template the icon (logo.icon) of each domain. Therefore, I needed the QIcon to be converted to a QString containing the base64 encoding of the image. Moreover, the distribution of the QT Framework I downloaded for Mac Os X seems to block some useful constructors for QString objects

I would like to share with you how I did it:

We convert the QIcon object to a QImage through another conversion to a QPixmap object at line 2. Then we create a QByteArray and a QBuffer associated to the byte-array. We call the save() method of the QImage object that saves the image in PNG format inside the byte-array, using the buffer.
At the end, we call the byteArray.base64() method that returns a copy of the byte-array, converted to base64. From that object we call its data() method to retrieve a char* representation of the byte-array, creating the QString.

Hope this could help who runs into my problem.

Published by

dgraziotin

Daniel Graziotin is a PhD student in Computer Science at the Free University of Bozen-Bolzano. His research interests include human aspects in empirical software engineering with psychological measurements, Web engineering, and Open Science. He is Editorial Associate at the Journal of Open Research Software and the local coordinator of the OKFN Open science Italian group . He is a member of the ACM, SIGSOFT, IEEE, and the IEEE Computer Society. Read more about.

2 thoughts on “Convert a QIcon (or just a QImage) to base64 and QString”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">