[英]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. 我一直试图找到一种方法来改变字体文件的行间距默认值,使用
QFont
, QFontMetrics
或类似的东西。 I'm using QPainter::drawText
to draw some text in a bounding rectangle. 我正在使用
QPainter::drawText
在边界矩形中绘制一些文本。
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. 奇怪的是,
QFont
允许更改字体字距,甚至有一些拉伸操作和字母间距,但没有什么可以改变行之间的默认空格。 I've searched and found some partial solutions using QTextLayout
but none seemed to work properly. 我搜索过并发现了一些使用
QTextLayout
部分解决方案,但似乎都没有正常工作。
I need to use QPainter
because I generate a texture with the text to be rendered with OpenGL. 我需要使用
QPainter
因为我生成一个纹理,其中包含要使用OpenGL渲染的文本。
Looking for more ideas for me to try out! 寻找更多的想法让我试试!
UPDATE UPDATE
I've found that I can use QPainter
to draw a QStaticText
which allows for HTML text formatting, similar to QTextDocument
. 我发现我可以使用
QPainter
绘制QStaticText
它允许HTML文本格式,类似于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. 但是,CSS样式在
QTextDocument
中QTextDocument
(有一个错误报告 )...因此仍然没有领先但我希望这能让我走上正轨。
SOLVED 解决了
I got what I wanted using QTextDocument
, like Mykhaylo suggested. 我得到了我想要的
QTextDocument
,就像Mykhaylo建议的那样。 Link to solution 链接到解决方案
QFontMetrics
was not designed particularly for multi-line text. QFontMetrics
并非专为多行文本而设计。
Use QTextDocument
. 使用
QTextDocument
。 You can print multi-line and rich text with it, even using QPainter
. 即使使用
QPainter
,您也可以使用它打印多行和富文本。 See the solution how to use QPainter with QTextDocument 请参阅解决方案如何将QPainter与QTextDocument一起使用
It seems there is not much that can be done here. 似乎这里没有太多可以做的事情。
QFontMetrics::lineSpacing
returns what you need but it is read-only. QFontMetrics::lineSpacing
返回您需要的内容,但它是只读的。
It's the sum of font height and leading. 它是字体高度和前导的总和。 You can adjust height - set it in
QFont
constructor. 您可以调整高度 - 在
QFont
构造函数中设置它。 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. 有些人将
\\n
添加到字符串的末尾以增加行之间的空间,但当然这并不总是一个好的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.