[英]QFont pixelSize and QFontMetrics height give different values
I am creating a custom label and want it to have a 13 pixel high text and setting label's max height to 14. 我正在创建一个自定义标签,并希望它有一个13像素高的文本,并将标签的最大高度设置为14。
textFont.setFamily("Frutiger LT Com 55 Roman");
textFont.setPixelSize(13);
setFont(textFont);
QFontMetrics fm(this->font());
QFontInfo fi(this->font());
qDebug()<<"QFontMetrics"<<fm.height()<<fm.xHeight()<<fm.ascent()<<fm.descent();
qDebug()<<"QFontInfo"<<fi.exactMatch()<<fi.family()<<fi.pixelSize();
This is what I get 这就是我得到的
QFontMetrics 16 3 13 3
QFontInfo true "Frutiger LT Com 55 Roman" 13
Why am I getting height as "16" when I am setting pixelSize as "13"? 当我将pixelSize设置为“13”时,为什么我将高度设为“16”? And because of this text on my label is getting clipped from bottom (Vertically center aligned). 因为我的标签上的文字从底部剪裁(垂直居中对齐)。
QFontMetrics "height()" definition says height=ascent+descent+1(for baseline). QFontMetrics“height()”定义表示height = ascent + descent + 1(基线)。 But as you can see above ascent=13, descent=3 and height=16. 但是你可以看到上面的ascent = 13,descent = 3和height = 16。 This contradicts "height()" function definition. 这与“height()”函数定义相矛盾。
This seems to be bug in documentation of Qt 5 (someone just forgot to change some Doxygen comments). 这似乎是Qt 5文档中的错误(有人只是忘了改变一些Doxygen评论)。
Commit that removes the +1
is here: https://qt.gitorious.org/qt/qtbase/commit/cb8445f0323b0eefbb04f1d8adad81a00b53abd8 删除+1
提交在此处: https : //qt.gitorious.org/qt/qtbase/commit/cb8445f0323b0eefbb04f1d8adad81a00b53abd8
The commit says: 提交说:
Remove historical +1 from font height calculation 从字体高度计算中删除历史+1
Historically, we've calculated font height as ascent+descent+1. 从历史上看,我们已经将字体高度计算为上升+下降+ 1。 In Qt 4, a patch was added to work around this by subtracting 1 from the descent of the font engines. 在Qt 4中,添加了一个补丁,通过从字体引擎的下降中减去1来解决这个问题。 We now remove the +1 and the work arounds. 我们现在删除+1和解决方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.