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

2

7

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.

Paradox answered 28/4, 2015 at 11:2 Comment(0)
S
3

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

Spinule answered 7/5, 2015 at 8:54 Comment(1)
It works like intended. Really awesome Mykhaylo :). Thanks for the help!Paradox
C
0

It seems there is not much that can be done here.

QFontMetrics::lineSpacing returns what you need but it is read-only.

It's the sum of font height and leading. You can adjust height - set it in QFont constructor. But you can't set leading.

Some people add \n to the end of string to increase space between lines but of course this is not always a good solution.

Chara answered 28/4, 2015 at 15:22 Comment(5)
Thank you for the quick awnser @demonplus. I'll check the QFont documentation and see if what you suggested solves my problem. I'll post back the results :)Paradox
I can't to find any QFont constructor which takes a height parameter... adding new line characters will mess the word wrap so that isn't a solution for me either. But thanks again for your time @Chara ;)Paradox
QFont::QFont(const QString & family, int pointSize = -1, int weight = -1, bool italic = false) - when you will change pointSize, height of the font will change alsoChara
I got confused with what you meant with height ^^. Line spacing will change with height but leading will stay the same :). So no solution still.Paradox
Yes, sorry. I tried to describe it in my answer. Qt allows to change height of font but you can't change leading.Chara

© 2022 - 2024 — McMap. All rights reserved.