[Qt-interest] QFont/QFontMetrics oddity
Girish Ramakrishnan
girish at forwardbias.in
Wed Feb 17 06:49:28 CET 2010
Hi Chris,
In general, unless otherwise it's a container class (like QList,
QVector) always create Qt objects _after_ QApplication is created. From
the docs "Note that a QApplication instance must exist before a QFont
can be used." (http://qt.nokia.com/doc/4.6/qfont.html). In your case,
gFont gets created before QApplication is created and I suspect that is
the problem.
Girish
Chris wrote:
> I ran into a painting problem in my application that I was able to
> reproduce with the following code:
>
> #include <QFontMetrics>
> #include <QtDebug>
> #include <QApplication>
> #include <QPaintEvent>
> #include <QWidget>
> #include <QPainter>
>
> const QFont gFont( "Arial", 9 );
>
> class Widget : public QWidget
> {
> protected:
>
> void paintEvent( QPaintEvent * )
> {
> QPainter painter( this
> );
>
>
> QFont font( "Arial", 9 ); //only used for comparison purposes
>
> painter.setFont( gFont );
> QFont pFont = painter.font();
>
> QFontMetrics m1( gFont );
> QFontMetrics m2( pFont );
> QFontMetrics m3( font );
>
> qDebug() << "The same?" << ( pFont == font );
> qDebug() << "m1 width:" << m1.width( "100" );
> qDebug() << "m2 width:" << m2.width( "100" );
> qDebug() << "m3 width:" << m3.width( "100" );
> }
> };
>
> int main( int argc, char *argv[] )
> {
> QApplication app( argc, argv );
>
> Widget w;
> w.show();
>
> return app.exec();
> }
>
> On my Windows box I get the following output:
> The same? true
> m1 width: 15
> m2 width: 21
> m3 width: 21
>
> Can anyone explain why the calculated with of the width of the font
> instantiated in the global scope would be different than the others?
More information about the Qt-interest-old
mailing list