Why does QFontMetrics::maxWidth return such a large value?

  • Windows 7 SP1
    MSVS 2010
    Qt 4.8.4

    I want to determine the maximum size of a QLineEdit widget knowing the maximum number of characters that must fit into it.

    Therefore, I want to use:
    @int QFontMetrics::maxWidth () const@

    Returns the width of the widest character in the font.

    But this:

    @#include <QApplication>
    #include <QFont>
    #include <QFontMetrics>
    #include <iostream>

    using std::cout; using std::endl;

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

    QFontMetrics metrics(QApplication::font()); 
    cout << "Default -     Max width: " << metrics.maxWidth() << " Width of X: " << metrics.width('X') << endl;
    const QFont f("Monospace", 8);
    QFontMetrics metrics2(f); 
    cout << "Monospace 8 - Max width: " << metrics2.maxWidth() << " Width of X: " << metrics2.width('X') << endl;
    const QFont f2("Cambria", 16);
    QFontMetrics metrics3(f2); 
    cout << "Cambria 16 -  Max width: " << metrics3.maxWidth() << " Width of X: " << metrics3.width('X') << endl;
    return 0;


    outputs this:

    @Default - Max width: 23 Width of X: 6
    Monospace 8 - Max width: 23 Width of X: 6
    Cambria 16 - Max width: 91 Width of X: 12@

    Question: Why is the maximum width so much larger than the width of 'X'?

    I understand the maximum width may be significantly larger than the width of an "X". But 8 times larger as with the Cambria font? This doesn't appear to be an issue on Debian 6: according to this bug report https://bugreports.qt-project.org/browse/QTBUG-13399 maxWidth outputs values such as 7 and not values 4-8 times larger.


  • From the docs:

    bq. QFontMetrics::maxWidth ()
    Returns the width of the widest character in the font.

    Unicode contains like ~1,000,000 different characters from many different languages (including Asian characters, Cyrillic characters and so on) plus mathematical symbols and other fancy stuff (for example: emoticons). So even if a given font covers only a small subset of the Unicode standard, why it shouldn't be possible that there exists some character in the font which is 8 times wider than an arbitrary character you have picked ("X") ???

    Did you open the "problematic" font in some "font viewer" application and checked whether it actually contains some "very wide" characters? Or is the character "X" really one of the widest characters in that font?

  • Yes, I opened Cambria in the SIL ViewGlyph program. The largest I could observe was U+FB04. And metrics2.width(0xFB04) =18, or only 50% greater than 'X'. I saw no character that was eight times as wide. The font has characters from u+0021 to U_FB04.

    I'm familiar with Unicode and CJK fonts. But, 8 times as wide? That seems a bit much. Is it possible that it is returning something besides pixels? Some version of 26.6? And does Debian 6 not support Unicode and hence why they have much smaller maxWidths?

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.