Quantcast
Channel: Jobs
Viewing all articles
Browse latest Browse all 18427

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

$
0
0
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?

Viewing all articles
Browse latest Browse all 18427

Trending Articles