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!

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

Community
  • 1
  • 1
Pedro Leal
  • 183
  • 1
  • 10

2 Answers2

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

Community
  • 1
  • 1
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.

huysentruitw
  • 27,376
  • 9
  • 90
  • 133
demonplus
  • 5,613
  • 12
  • 49
  • 68
  • 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 :) – Pedro Leal Apr 29 '15 at 08:37
  • 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 @demonplus ;) – Pedro Leal Apr 30 '15 at 10:43
  • 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 also – demonplus Apr 30 '15 at 11:42
  • 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. – Pedro Leal Apr 30 '15 at 17:06
  • Yes, sorry. I tried to describe it in my answer. Qt allows to change height of font but you can't change leading. – demonplus Apr 30 '15 at 17:11