-1

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.

Andy Brice
  • 2,297
  • 1
  • 21
  • 28
  • Might be unrelated, but when changing from Qt 5.4? to Qt 5.11 styles in my Windows app got changed - font changed, some buttons looked crap. I had to manually set `QApplication` style, see here: http://doc.qt.io/qt-5/qapplication.html#setStyle-1 (maybe this is what happened to you and, simply, the default platform got changed). – hauron Sep 30 '18 at 18:21
  • I tried changing the style from Fusions to Windows Vista and restarting. But it didn't make any difference. – Andy Brice Sep 30 '18 at 20:29
  • You'd need to do that prior to creating `QApplication` instance. If it makes no difference what style you use, likely you didn't really change it. – hauron Oct 01 '18 at 06:38
  • The appearance (buttons etc) is visibly changed. – Andy Brice Oct 01 '18 at 07:56

1 Answers1

2

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?

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • I convert to pixels for use in QGraphicsCanvas, because otherwise output to PDF didn't work correctly. However customers think of fonts size in points, not pixels. And I don't think the standard (native) font dialogs allow you to choose a font size in pixels. So they choose the font size in points and I convert to pixels for QGraphicsCanvas. It is a bit of a kludge and possibly not a great decision, but this is a mature piece of software and I am pretty much stuck with it. – Andy Brice Oct 04 '18 at 19:09
  • You know that the DPI is the conversion factor, so you need to apply the inverse relative scaling to your canvas. Then the physical size on the canvas will be the same, but logical size in pixels won't be - and doesn't have to be. – Kuba hasn't forgotten Monica Oct 04 '18 at 19:24