[Qt-interest] QFont/QFontMetrics oddity

Chris thatchman at gmail.com
Tue Feb 16 20:19:40 CET 2010


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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100216/1ba3f403/attachment.html 


More information about the Qt-interest-old mailing list