Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why does QFontMetrics::maxWidth return such a large value?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Therefore
    wrote on last edited by
    #1

    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.

    Thoughts?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on last edited by
      #2

      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?

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Therefore
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved