QFontInfo::pixelSize() changes between Qt 4 and Qt 5

585 Views Asked by At

This code:

QFont convertPointToPixelSize( const QFont& f )
{
    QFont ret( f );
    QFontInfo fi( ret );
    ret.setPixelSize( fi.pixelSize() );
    return ret;
}
qDebug() << "getFont()=" << getFont();
qDebug() << "convertPointToPixelSize( getFont() )=" << convertPointToPixelSize( getFont());

Returns this on Qt 4.8.5:

getFont()= QFont( "MS Shell Dlg 2,36,-1,5,50,0,0,0,0,0" ) 
convertPointToPixelSize( getFont() )= QFont( "MS Shell Dlg 2,-1,48,5,50,0,0,0,0,0" ) 
getFont()= QFont( "Arial,28,-1,5,50,0,0,0,0,0" ) 
convertPointToPixelSize( getFont() )= QFont( "Arial,-1,37,5,50,0,0,0,0,0" ) 
getFont()= QFont( "Times New Roman,72,-1,5,50,0,0,0,0,0" ) 
convertPointToPixelSize( getFont() )= QFont( "Times New Roman,-1,96,5,50,0,0,0,0,0" )

And this on Qt 5.11.1:

getFont()= QFont( "MS Shell Dlg 2,36,-1,5,50,0,0,0,0,0" )
convertPointToPixelSize( getFont() )= QFont( "MS Shell Dlg 2,-1,72,5,50,0,0,0,0,0" )
getFont()= QFont( "Arial,28,-1,5,50,0,0,0,0,0" )
convertPointToPixelSize( getFont() )= QFont( "Arial,-1,56,5,50,0,0,0,0,0" )
getFont()= QFont( "Times New Roman,72,-1,5,50,0,0,0,0,0" )
convertPointToPixelSize( getFont() )= QFont( "Times New Roman,-1,144,5,50,0,0,0,0,0" )

Note the different QFontInfo::pixelSize() values returned for the same fonts. So QFontInfo::pixelSize() seems to have changed between Qt 4 and Qt 5 (returns bigger values for Qt 5). Is there anything I can do to get the values of QFontInfo::pixelSize() for Qt 4 in Qt 5? I looked at QFontMetrics, but I didn't see anything useful.

1

There are 1 best solutions below

2
On

QFont is a font request, it doesn't indicate the actual font selected. QFontInfo provides that. All that you have shown is that Qt 5 and Qt 4 use different DPI values on your system, and that's to be expected. Points are a physical unit, while pixels are a logical unit. DPI links the two. If you want same pixel sizes, why didn't you choose choose the desired pixel size in the font request (QFont), instead of a point size?