简体   繁体   中英

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:

 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!

The problem was in rounding. If I use QFontMetricsF instead of QFontMetrics results are correct

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). It looks like bug in Qt. https://bugreports.qt.io/projects/QTBUG/issues/QTBUG-73458?filter=allopenissues

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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