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

C

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:

QIcon icon = mostVisited.at(i).icon; // use your method to retrieve the QIcon object
QImage image(icon.pixmap(20,20).toImage()); // 20,20 are the size of the icon
QByteArray byteArray;
QBuffer buffer(&byteArray);
image.save(&buffer, "PNG"); // writes the image in PNG format inside the buffer
QString iconBase64 = QString::fromLatin1(byteArray.toBase64().data());

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.

About the author

dgraziotin

Dr. Daniel Graziotin is a senior researcher (Akademischer Rat) at the University of Stuttgart, Germany. His research interests include human, behavioral, and psychological aspects of empirical software engineering, studies of science, and open science. He is associate editor at the Journal of Open Research Software and academic editor at the Research Ideas and Outcomes (RIO) journal. Daniel was awarded an Alexander von Humboldt Fellowship for postdoctoral researchers in 2017, the European Design Award (bronze) in 2016, and the Data Journalism Award in 2015. He received his Ph.D. in computer science at the Free University of Bozen-Bolzano, Italy.

3 comments

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About Author

dgraziotin

Dr. Daniel Graziotin is a senior researcher (Akademischer Rat) at the University of Stuttgart, Germany. His research interests include human, behavioral, and psychological aspects of empirical software engineering, studies of science, and open science. He is associate editor at the Journal of Open Research Software and academic editor at the Research Ideas and Outcomes (RIO) journal. Daniel was awarded an Alexander von Humboldt Fellowship for postdoctoral researchers in 2017, the European Design Award (bronze) in 2016, and the Data Journalism Award in 2015. He received his Ph.D. in computer science at the Free University of Bozen-Bolzano, Italy.