Unsolved Load QIcon with QByteArray containing SVG vector image without creating file?
-
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.
-
Hi
There might be a way via
http://doc.qt.io/qt-5/qpixmap.html#loadFromData-1
Then into QIcon.
Not tested :) -
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.
-
@aatwo
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. -
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.