QFont does not allow line spacing or can be set manually

I am trying to find a way to change the default value for a font string string using QFont , QFontMetrics or something like that. I am using QPainter::drawText to draw some text in a bounding box.

It is strange that QFont allows QFont to change the font of kerning and even has some stretching operation and the distance between the letters, but nothing to change the default space between the lines. I searched and found some partial solutions using QTextLayout , but none of them worked properly.

I need to use QPainter because I am creating a texture with text that will be displayed using OpenGL.

Looking for more ideas for me to try!

UPDATE

I found that I can use QPainter to draw a QStaticText , which allows you to format text in HTML format, similar to QTextDocument . However, CSS styling does not work, as in QTextDocument (there is an error report there) ... Therefore, there is still no lead, but I hope this puts me on the right track.

solvable

I got what I wanted to use QTextDocument , as Michaelo suggested. Solution Link

+6
source share
2 answers

QFontMetrics not designed specifically for multi-line text.

Use a QTextDocument . You can print multiline and rich text with it, even using QPainter . See Deciding how to use a QPainter with a QTextDocument.

+3
source

It seems that little can be done here.

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

This is the sum of the font height and the lead. You can adjust the height - set it in the QFont constructor. But you cannot establish a lead.

Some people add \n to the end of a line to increase line spacing, but of course this is not always a good solution.

0
source

Source: https://habr.com/ru/post/986128/


All Articles