简体   繁体   English

QFontMetrics给等宽字体带来奇怪的结果

[英]QFontMetrics give strange results for monospace font

Сan someone explain to me results of this test program? 有人向我解释该测试程序的结果吗?

#include <QApplication>
#include <QDebug>
#include <QFontMetrics>
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QFont font;
    font.fromString("Monospace,14");
    QFontMetrics fm(font);
    qDebug() << "W       : " << fm.width('W');
    qDebug() << "8*W     : " << 8*fm.width('W');
    qDebug() << "WWWWWWWW: " << fm.width("WWWWWWWW"); // 8*W
    return 0;
}

After comipiling this code with Qt5.11 I have such results: 在将此代码与Qt5.11结合之后,我得到了这样的结果:

 W : 11 8*W : 88 WWWWWWWW: 92 

Size of one character 'W' for this monospace font is 11. I expect that size of string that consists of 8 such characters should be 88. But QFontmetrics::width returns 92! 此等宽字体的一个字符'W'的大小为11。我希望由8个此类字符组成的字符串的大小应为88。但是QFontmetrics :: width返回92!

The problem was in rounding. 问题出在四舍五入。 If I use QFontMetricsF instead of QFontMetrics results are correct 如果我使用QFontMetricsF而不是QFontMetrics结果是正确的

W       :  11.4375
8*W     :  91.5
WWWWWWWW:  91.5

But I found another strange thing. 但是我发现了另一件事。 QFontMetricsF::maxWidth() should returns qreal type, but in fact it always returns rounded value (11 in my example). QFontMetricsF::maxWidth()应该返回qreal类型,但实际上它总是返回舍入值(在我的示例中为11)。 It looks like bug in Qt. 它看起来像Qt中的错误。 https://bugreports.qt.io/projects/QTBUG/issues/QTBUG-73458?filter=allopenissues https://bugreports.qt.io/projects/QTBUG/issues/QTBUG-73458?filter=allopenissues

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM