Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

UPDATE

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 but I hope this puts me on the right track.

SOLVED

I got what I wanted using QTextDocument, like Mykhaylo suggested. Link to solution

like image 717
Pedro Leal Avatar asked Apr 28 '15 11:04

Pedro Leal


1 Answers

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

like image 73
Mykhaylo Kopytonenko Avatar answered Sep 28 '22 15:09

Mykhaylo Kopytonenko