Passing SVG from C++ to QML
-
How do I pass an SVG which I currently have in memory on the C++ side (QByteArray or similar) on to an Image component in QML?
For other file types such as PNG or JPEG, I would simply use QImage (or QPixmap) and a QQuickImageProvider. But that class doesn't seem to have an interface for SVGs.
What options do I have?
-
You can paint a SVG on a QImage with QSvgRenderer and then use QQuickImageProvider.
-
@GrecKo
Thank you. That is of course an option.
It just sounds awfully complicated, given that I can provide other SVGs to the Image components simply by providing a file or resource name. Especially if the Image components dynamically resizes. -
@Asperamanca said in Passing SVG from C++ to QML:
QQuickImageProvider
You can simply pass the SVG as string to the QML side and show it using a data URI scheme (it is supported by QML Image).
Image { ... source: "data:image/svg+xml;utf8," + svgString ... }
You can also do the same thing for other image formats. As most other formats are binaries, you have to convert them to Base64 string first. By the way, IMHO using QQuickImageProvider seems to be a better solution in that case.
Image { ... source: "data:image/png;base64," + pngBase64String ... }
-
@Soheil
Do you happen to know if this blob-encoding is an officially supported way to display images?
For large images, the extra work of encoding the byte array into a QUrl seems excessive. For small SVGs, this might be a good solution for me.