How to get the font size (in pxl) of a text with paragraphs?
Unsolved
General and Desktop
-
wrote on 7 Aug 2017, 16:48 last edited by Niagarer 8 Jul 2017, 16:49
Hey,
I already saw, that I can use QFontMetrics for a single line.
QFontMetrics tries to do it all in one line.
But I have a text with paragraphs and have to figure out, how big the rectangle has to be.
How can I do this?
Thanks for answers -
wrote on 7 Aug 2017, 22:52 last edited by A Former User 8 Jul 2017, 22:54
Hi! In my experience this is a very tricky thing. If your intention is to draw the text with QPainter, I'd recommend to drop QFontMetrics and instead render the text to an "offscreen" image first to get the bounding rect. Something like:
static QRectF getBoundingBox(QString const &s, QFont const &f) { QImage image(1, 1, QImage::Format_Mono); QPainter painter(&image); painter.setFont(f); QRectF rr; painter.drawText(QRectF(0, 0, INT_MAX, INT_MAX), Qt::TextWordWrap, s, &rr); return rr; }
2/2