Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Load QIcon with QByteArray containing SVG vector image without creating file?
aatwo last edited by
Hey guys. I have an SVG (vector) image in a Qt resource file and I am creating my applications QIcon with the SVGs resource path as a parameter. This works as expected and allows me to use one resource to supply my application icon in any size on any platform, rather than providing a ton of raster images.
I have also written some code that can modify this icon dynamically using Qts SVG module, the end result of which is a QByteArray containing my modified SGV icon. However to load this into a QIcon I currently have to first write it to disk (using QTemporaryFile) and then create the QIcon using the temporary file path.
I feel this disk write is an unecessary overhead and try to avoid disk writes where possible, so was wondering if Qt offers anything that will let me achieve this entirely in memory?
Kind regards, Aaron.
There might be a way via
Then into QIcon.
Not tested :)
aatwo last edited by
Hey. Thanks for the reply. I don't believe QPixmap::loadFromData() or QImage::loadFromData() work with vector based images since QPixmap and QImage are both pixel based image classes.
It is possible however to render SVG images onto any paint device (including QPixmap and QImage) using the QSvgRender class, but again I want to avoid this method since loading a QIcon with a QPixmap would loose all benefits of having the icon in vector format as mentioned in my initial post.
The problem with your requirement is that icons are platform and device dependent, so this wouldn't be possible. However you can render the SVG in whatever sizes you need and then convert the images to proper icons. This way you get what you want - good quality over a span of sizes.
VRonin last edited by VRonin
What I would do is reimplement QIconEngine rendering the SVG in the paint method using QSvgRender and then pass this class to the constructor of your QIcon
Yes, that's a good idea.