Important: Please read the Qt Code of Conduct -

How to get the font size (in pxl) of a text with paragraphs?

  • 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

  • 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);
        QRectF rr;
        painter.drawText(QRectF(0, 0, INT_MAX, INT_MAX), Qt::TextWordWrap, s, &rr);
        return rr;

Log in to reply