So I am using some SVG files to show graphics in my Qt application. I load the svg file just fine into a QSVgRenderer, but when I actually render the SVG data on screen it is smaller than expected.
To make sure I wasn't doing something odd I do the following inside an empty QDialog PaintEvent function using text only below to show my issue.
QPainter p(this);
QFont f("verdana", 9);
p.setFont(f);
painter.translate(100,100);
painter.setPen("#FF0000");
QRectF br = painter.boundingRect(0,0,0,0, Qt::AlignLeft, "Hello There!);
painter.drawRect(0, -br.height(), br.width(), br.height());
painter.drawText(0,0,"Hello There!");
painter.setPen("#000000");
QByteArray svgData(QString().sprintf(
"<svg><text font-family=\"%s\" font-size=\"%dpt\" fill=\"black\">%s</text></svg>", "verdana", 9, "Hello There!"
).toStdString().c_str())
QSvgRenderer svgr(svgData);
// svgr.setViewBox(QRectF(0,0,br.width(), br.height()); // Tried this line also but did not make any difference.
// svgr.setViewBox(QRectF(0,0,br.width() * 0.68, br.height()); // Sort of makes my svg the proper width, but breaks when using other font styles.
svgr.render(&p, br);
The result I get looks like this:
Here is what it looks like with the drawText line commented out since that line is only showing the bounding rect was calculated properly:
I am looking for it to be like this:
where the "Hello There!" is the SVG item scaled to fit properly into the bounding rect area. I thought I had it working at one point by setting the viewBox to the same size as the bounding rect, but it does not work properly now unless I scale the bounding rect to be smaller.
I also tried setting width and height on the <svg>
tag but that did not make a difference.
Am I doing something wrong?