Question

I've been having a little trouble with QSvgRenderer and text elements; every time I call boundsOnElement(), I get back a QRect that correctly corresponds to the top left corner of the text element (after applying matrixForElement(), of course), but its width and height are always zero.

I'm using PySide, so it's possible that some unimplemented layer of Python is at fault, but poor support of SVG text in Qt and elsewhere makes me think otherwise. Has anyone:

  • experienced this using Qt directly,

and, more importantly:

  • have any idea how to get the size of an SVG text element in Qt?

I'm considering trying to guess manually, based on the font size, length of the string, etc. but this seems really error-prone and I'd like to do it correctly if it can be done. If anyone has any typesetting experience, pointers on things I'll need to consider if I have to hack this are certainly welcome.

Was it helpful?

Solution

I have also experienced this bug. One alternative is to use the QFontMetrics class. (or QFontMetricsF class)

Example:

QSvgGenerator svg;
QFont font("Arial", 12);
QFontMetricsF metrics(font, &svg);

QString text = "My SVG String";
QRectF bounds = metrics.boundingRect(text);

This will return non-zero bounds. However, in my experience, while this routine will return accurate origin and height information, the width is typically longer than it should be. The longer your text string is, the more 'padding' will appear at the end of this bounding box. If anyone has a solution for that bug, please let me know.

Update: This sizing bug is unique to particular fonts. For example: Times New Roman appears to be computed very accurately, whereas other fonts may be computed too short or too long. This seems to imply there is a particular trait in some fonts that is being overlooked by the algorithm Qt is using.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top