Search code examples
c++qtfontsspacingfontmetrics

QFont doens't allow line spacing or leading to be set manually


I've been trying to find a way to change a font file's line spacing default value, using QFont, QFontMetrics or something like that. I'm using QPainter::drawText to draw some text in a bounding rectangle.

It's strange that QFont allows for font kerning to be changed and even has some stretch operation and letter spacing, but nothing to change the default space between lines. I've searched and found some partial solutions using QTextLayout but none seemed to work properly.

I need to use QPainter because I generate a texture with the text to be rendered with OpenGL.

Looking for more ideas for me to try out!


I've found that I can use QPainter to draw a QStaticText which allows for HTML text formatting, similar to QTextDocument. However, CSS styling doesn't work like in QTextDocument (there's a bug report). Therefore, still no leading.


Solution

  • QFontMetrics was not designed particularly for multi-line text.

    Use QTextDocument. You can print multi-line and rich text with it, even using QPainter. See the solution how to use QPainter with QTextDocument