Reputation: 585
С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!
Upvotes: 5
Views: 577
Reputation: 585
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
Upvotes: 3