QPixmap and SVG
Asked Answered
P

4

12

How would you suggest to handle svg with QPixmap?

The construct QPixmap(":/myfile.svg"); then call of scaled() does not work. The QPixmap gets pixelised.

Thx.

Poinsettia answered 9/4, 2012 at 19:58 Comment(0)
V
9

You should use SVGRenderer to render it onto a QImage. From there you can convert to a QPixmap with QPixmap::convertFromImage.

Vicariate answered 9/4, 2012 at 20:12 Comment(2)
Thx, I did like suggested here : #8552190Poinsettia
It is possible to create QPainter over QPixmap and render SVG directly to the QPixmap.Cyclist
S
40

Now there is much simpler way without needing of SVG module

QIcon("filepath.svg").pixmap(QSize())

So simple and works fine. Atleast in my case it worked.

Springing answered 29/4, 2016 at 10:42 Comment(3)
At least with respect to the limited use case of icons, this should now be the accepted answer. Note that the QIcon::addFile() method also implicitly accepts SVG files now, permitting each icon mode and state to be associated with its own SVG file.Irreligion
This does not scale the SVG up, if it was smaller than this size, it stays the same.Deepdyed
This is great. But how do you achieve the same thing by creating a scalable icon from an bytearray containing SVG?Taxi
V
9

You should use SVGRenderer to render it onto a QImage. From there you can convert to a QPixmap with QPixmap::convertFromImage.

Vicariate answered 9/4, 2012 at 20:12 Comment(2)
Thx, I did like suggested here : #8552190Poinsettia
It is possible to create QPainter over QPixmap and render SVG directly to the QPixmap.Cyclist
C
6

Something like that:

QSvgRenderer renderer(svg_file_name);
QPixmap pm(width, height);
pm.fill(fill_color);
QPainter painter(&pm);
renderer.render(&painter, pm.rect());
Cyclist answered 30/7, 2014 at 6:22 Comment(0)
G
1

On Qt5.12 i managed to display SVG icons without pixellisation in a QLabel:

QPixmap logoPixmap(":my-logo.svg"); // set your logo here
auto logoLabel = new QLabel(this);
logoLabel->setPixmap(logoPixmap);
logoLabel->setScaledContents(true);
logoLabel->setFixedSize(176, 61); // set your size here
Glassware answered 21/2, 2023 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.