PDA

View Full Version : QFontMetricsF returns integer width under Windows (Qt 5.2.1)



ABL
20th May 2014, 11:29
Hello to everybody.

I am using Qt 5.2.1 under Windows 8.1 (64bits, but using 32bits MinGW and 32bits Qt 5.2.1) and under Linux Mint 13 (64bits, using 64bits Qt 5.2.1).
I am having troubles with the values given by QFontMetricsF under Windows.
In particular, whatever HintingPreference I am setting, I always obtain integer values (actually, real values, but with 0 decimal part).
Here is a minimal example:


#include <QApplication>
#include <QtGui>
#include <QFont>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QFont font("Arial");
font.setWeight(QFont::Normal);
font.setItalic(false);
font.setStyleStrategy(QFont::NoFontMerging);
font.setHintingPreference(QFont::PreferNoHinting);
font.setPointSizeF(12.231);
font.pixelSize();

QFontMetricsF fmf(font);
QFontMetrics fm(font);

QString aa = "ABC";

qreal dim1 = fmf.width(aa);
int dim2 = fm.width(aa);
qDebug() << dim1;
qDebug() << dim2;

return 0;
}


When I run this code under Linux, I obtain, as expected:
- with "QFont::PreferVerticalHinting" or "QFont::PreferNoHinting"
32.8906 (from QFontMetricsF)
33 (from QFontMetrics)

- with "QFont::PreferFullHinting"
34 (from QFontMetricsF)
34 (from QFontMetrics)

However, when I run it under Windows, I always obtain:
37 (from QFontMetricsF)
37 (from QFontMetrics)
even with "QFont::PreferVerticalHinting" or "QFont::PreferNoHinting".

Is it a bug?
I searched the bug tracker and found these:
https://bugreports.qt-project.org/browse/QTBUG-26822
https://bugreports.qt-project.org/browse/QTBUG-62
The first reports the behavior under Linux, which is the same I see; but under Windows I always obtain integer values. The second bug report was closed (fixed?)

I also tried to search in the forum and found this:
http://www.qtcentre.org/threads/10103-Only-integers-from-QFontMetricsF-width
but I didn't really understand the answer.

Is there a work-around to extract the actual qreal value of text width?

The problem I am facing arises from the fact that in the main program the text is rendered by drawText() and possibly scaled, and the integer width given by QFontMetricsF::width, used to compute the position of symbols, produces artifacts in the relative alignment of symbols.

Thank you very much for your help and please forgive my questions if they sound naive, but I know very little of the Qt framework.

Ciao,
ABL