1

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:

enter image description here

Here is what it looks like with the drawText line commented out since that line is only showing the bounding rect was calculated properly:

enter image description here

I am looking for it to be like this:

enter image description here

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?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
MrJman006
  • 752
  • 10
  • 26
  • You are not the first to run into this bug... :( Here is my old question, with a partial answer that I have also updated: http://stackoverflow.com/questions/34183996/saving-a-qgraphicsscene-to-svg-changes-scaling. I also created a bug report about it: https://bugreports.qt.io/browse/QTBUG-49892 – Thalia Mar 14 '16 at 22:28
  • You are right about having to scale the bounding rect to be smaller - but you may also run into rounding issues if you require accuracy. – Thalia Mar 14 '16 at 22:30
  • yeah for now i'm using the scale method as i am not yet at a point where complete accuracy matters. Itried a few other things with no success. maybe ill look at Qt src when i get some time and see if I can find the problem. Thanks for the guidance! – MrJman006 Mar 15 '16 at 01:50
  • Looking at your example - my trouble had been with the SVGgenerator, never with the renderer. Have you tried setting a size on your svg xml ? – Thalia Mar 15 '16 at 14:11
  • @Thalia if you are refering to then yes I tried that at one point. I will try again to verify. – MrJman006 Mar 21 '16 at 21:42
  • @Thalia I had a chance to play around with it again yesterday. I double checked and the width/height on the tag did not have any influence. – MrJman006 Mar 23 '16 at 15:24
  • I'm sorry I don't know much more - perhaps try playing with the font size units ? https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/font-size – Thalia Mar 23 '16 at 15:37

0 Answers0