[Qt-interest] Getting the width of the font w/ QFontMetrics

Paul England pengland at cmt-asia.com
Thu Jan 14 06:53:41 CET 2010


This never works for me:

QFont font( "Tahoma", 10 );
QLineEdit* line_edit;
line_edit->setText( "Blah" );
line_edit->setFont( font )

font.setPointSize( 16 );
line_edit->setFont( font );

QFontMetrics fm( font );
int width = fm.width( line_edit->text() );
line_edit->setFixedWidth( width );

// It's always too narrow.

I even tried
int width = fm.boundingRect( line_edit->text() ).width();
and get roughly the same thing... slightly differen,t but
far too narrow. 

Am I missing something?




More information about the Qt-interest-old mailing list