Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Show scaled svg
jss193 last edited by jss193
I am trying to display a svg image on my screen, but it does not appear scaled ( I just can see a piece of the image), I mean it is not scaled, I tried the following code:
QImage image; image.load("pathto.svg"); QPainter painter(this); painter.drawImage(QPoint(10,10),image);
I tried the following too:
QImage image(QString("pathto.svg")); QPainter painter(&image); painter.drawImage(QPoint(10,10),image);
In first case the image is not resized and I dont know how to do it, can someone help me?
In the second case the image does not even appear if I dont use this pointer, I dont well understand why, could you explain me why using this instead of &image??
Why not just load the svg in a QLabel ?
In the second case, you are painting
jss193 last edited by
Hi, I also tried but it shows the same, the image is partially showed, what I wish is to (for instance) show svg image scaled to qlabel, what is, shown completely and scaled to qlabel size.
Isn't the scaledContents property what you want ?
you need to tell QPainter a size, so that it knows what to size the image to. Take a look at the function parameters
void QPainter::drawImage(const QRectF & target, const QImage & image, const QRectF & source, Qt::ImageConversionFlags flags = Qt::AutoColor)
target = the rectangle to be drawn into
image = the image to be drawn
source= the target rectangle of the image, in case you only want part of it
the function you're using is
void QPainter::drawImage(const QPoint & point, const QImage & image)
that completely ignores any size adjustments :)
Buckwheat last edited by
As @SGaist stated, you can always put the SVG item into a label or a button as its image.
Have you tried QSvgRenderer instead of loading the svg file into an image? You will need to add QT += svg to your PRO file and include <QSvgRenderer> in your file.
// Setup the painter to the QWidget (or use an image instead of a widget)
QPainter painter (this);
// Create the SVG object
QSvgRenderer renderer ("file.svg");
// Set the viewing box
renderer.setViewBox (painter.viewport ());
// Now set the scale or other transforms into the painter
// Render the image
The problem with going into an image is that it will create either a MEGA huge image or a small image or a normal image based on the SVG document size. Then, scaling this image you lose all the nice smoothing and crispness using an SVG gives you. It is best to scale the SVG to the size you need and not the other way around.
I actually load an SVG into an image like you are doing and use it as a background for painting. This is actually faster than drawing the SVG each time (for my case) and the image looks crisp since the SVG is scaled and not the image.