Nominate our 2022 Qt Champions!

QFontMetricsF under Windows (qt 5.2.1 & 5.3.0)

  • Hello to everybody.

    I found a strange behavior of QFontMetricsF under Windows, but I don't know if it is a bug or expected behavior.
    In particular, when I use QFontMetricsF::width() to determine the width of a text string, I always obtain an integer value (or to be precise, a real value with 0 decimal part) for whatever font I am using and HintingPreference.
    I tried with Qt 5.2.1 and Qt 5.3.0 under Windows 8.1 (64bits, but using 32bits MinGW and 32bits Qt 5.2.1 or Qt 5.3.0) and with Qt 5.2.1 under Linux Mint 13 (64bits, using 64bits Qt 5.2.1).
    Here is a minimal example:
    #include <QApplication>
    #include <QtGui>
    #include <QFont>

    int main(int argc, char *argv[])
    QApplication app(argc, argv);
    QFont font("Arial");
    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:
    -- with "QFont::PreferVerticalHinting" or "QFont::PreferNoHinting"
    32.8906 (from QFontMetricsF) and 33 (from QFontMetrics)
    -- with "QFont::PreferFullHinting"
    34 (from QFontMetricsF) and 34 (from QFontMetrics)

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

    In the bug tracker I found these:
    The second report seems to be solved. The first report, if I understood correctly the comments, explains the behavior I see under Linux, which is then expected behavior, but contrary to what is expected for QFont::PreferNoHinting, under Windows I always obtain an integer value.

    Do you know if there is a work-around to obtain an actual real value of the text width also under Windows?

    This question arises from the fact that I am trying to contribute to a program in which symbols from a font are rendered and scaled by drawText(), and the width given by QFontMetricsF::width is used to compute the relative position of the symbols. The fact that this width is always an integer value produces positioning artifacts in the relative alignment of symbols under Windows.

    Thank you very much for your help.
    Best regards,

Log in to reply