Solved Qt 5.14 Updates to SVG Rendering
-
I have a function that creates a QImage from an SVG file; with the size parameter being the size of the QImage and the box parameter being the position and size of the rendered svg within that image:
QImage fromSvg(const QString& path, const QSize& size, const QRect& box) { QSvgRenderer renderer(path); QImage image(size, QImage::Format_ARGB32); image.fill(QColor(0, 0, 0, 0)); QPainter painter(&image); renderer.render(&painter, box); return image; }
This was working fine until I updated to Qt 5.14, where it appears a change was made regarding how SVGs are rendered: https://github.com/qt/qtsvg/commit/c3ba57620c2c40d285004af2e1809e581e6bb7e7#diff-af44df228dc853c5da144ba0cb04f6a5
I'm not much of a graphics expert; how can I update this to work with the new SVG rendering?
-
@Qt_User72653 I think you are facing with this issue: QTBUG-81259
This should be revert in next coming release (Qt 5.14.1), which should be at January 21st. -
Hi
What is the effect of this change ?
How does it look different from pre 5.14 ?Did you open the svg in say InkScape and look how the viewbox is ?
(in inkscape its the page size/box) -
The majority of the time I'm trying to center the svg in the middle of the of the QImage, so pre 5.14 I could call it like so and expect the svg to be rendered in the middle of the image:
fromSvg("file.svg", {20, 20}, {5, 5, 10, 10}); // image is 20x20, svg is rendered 10x10 at (5, 5).
With 5.14, some images are rendered with the top left of the svg at the center of the QImage, and some svgs are rendered shifted up and to the left. I tried inputting various values to try to get a sense of how the new svg rendering works, but I couldn't find any consistency to it.
I've looked at the svg files and they're all sized so the viewbox contains the image without any extra whitespace.
-
I wound up creating a QIcon from an SVG then using the icon to generate a pixmap; I then draw that pixmap on top of the base image.
-
@Qt_User72653 I think you are facing with this issue: QTBUG-81259
This should be revert in next coming release (Qt 5.14.1), which should be at January 21st. -
Perfect! Thank you.