[Qt-interest] How to create QFont from QFontInfo

Tony Rietwyk tony.rietwyk at rightsoft.com.au
Tue Jun 1 04:17:52 CEST 2010


Hi Everybody, 

For some strange reason on Windows, QListWidget uses the icon font from the user settings, instead of the standard application font - like most other widgets.  

So I use an override class to copy the font from the parent to the widget in the Polish event.  The problem is that just copying the parent font doesn't work - since the font has not been resolved.  I have to create a QFontInfo from the parent font, then individually copy all of the font settings from the fontinfo, before setting the widget font: 

			QFontInfo fi( parentWidget()->font() );
			QFont f( fi.family(), fi.pointSize(), fi.weight(), fi.italic() );
			f.setFixedPitch( fi.fixedPitch() );
			f.setOverline( fi.overline() );
			f.setRawMode( fi.rawMode() );
			f.setStrikeOut( fi.strikeOut() );
			f.setStyle( fi.style() );
			f.setStyleHint( fi.styleHint() );
			f.setUnderline( fi.underline() );
			setFont( f );

It seems silly for me to have to update this if any other settings are added to QFont in future.  Either a QFont constructor taking QFontInfo, or a static fromFontInfo function would be nice.  

Thanks.





More information about the Qt-interest-old mailing list