Matthieu Riegler
Matthieu Riegler

Reputation: 54998

QPixmap and SVG

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.

Upvotes: 12

Views: 26751

Answers (4)

mmerle
mmerle

Reputation: 595

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

Upvotes: 1

Yash
Yash

Reputation: 7064

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.

Upvotes: 40

Sergey Galin
Sergey Galin

Reputation: 436

Something like that:

QSvgRenderer renderer(svg_file_name);
QPixmap pm(width, height);
pm.fill(fill_color);
QPainter painter(&pm);
renderer.render(&painter, pm.rect());

Upvotes: 6

Troubadour
Troubadour

Reputation: 13421

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

Upvotes: 9

Related Questions