Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QFont::fromString() change between Qt 4 and Qt 5



  • When I convert this string:

    MS Shell Dlg 2,36,-1,5,50,0,0,0,0,0
    

    Using QFont::fromString() in Qt 4.8.6 I get font:

    MS Shell Dlg 2/Normal/36pt
    

    But when I convert the same string using QFont::fromString() in Qt 5.11.1 on the same Windows 7 development machine I get font:

    MS Shell Dlg 2/Regular/36pt
    

    They look quite different sizes, which is an issue for backward compatibility. In QFontDialog on Qt 5 'Normal' isn't listed as an option for MS Shell Dlg 2.

    Similarly Arial/Normal/36pt gets converted to Arial/Regular/36pt.

    Is this a bug or just change I am going to have to live with? Is there a workaround for backward compatibility?

    --
    Andy Brice
    https://www.perfecttableplan.com


  • Moderators

    @AndyBrice
    One thing that has drastically changed from Qt4 to Qt5 is the QPA (Qt Platform Abstraction).
    Along with it a so called (internal) class QPlatformFontDatabase introduced. So it's very likely that some fundamentals regarding resolution of fonts also have changed.

    Using your example MS Shell Dlg 2,36,-1,5,50,0,0,0,0,0 and comparing the implementations across Qt also shows not much differences:

    Qt5:
    MS Shell Dlg 2 -> font family
    36 -> point size
    -1 -> pixelsize
    5 -> StyleHint
    50 -> Font weight
    0 -> style
    0 -> underline
    0 -> strikeout
    0 -> fixed pitch
    0 -> <not-used?>

    Qt4:
    MS Shell Dlg 2 -> font family
    36 -> point size
    -1 -> pixelsize
    5 -> StyleHint
    50 -> Font weight
    0 -> style
    0 -> underline
    0 -> strikeout
    0 -> fixed pitch
    0 -> raw mode

    The enum QFont::StyleHint hasn't changed. But the QFont::Weight was extended by various values.
    I would try to play around with those values. Maybe a value of 50 (=Normal) in Qt4 can be easily mapped to a new value in Qt5.
    Anyway i think the changes are height that there is no straight forward workaround for your issue.



  • The differences are significant. For example look at the big text which is Arial Italic 72, on Qt 4 versions of my app:

    0_1537889573979_cap1.jpg

    And Arial Italic 72, on Qt 5 versions of my app:

    0_1537889587678_cap2.jpg

    It isn't a showstopper though. Users will just have to modify the fonts if they move a plan from PerfectTablePlan v5 to PerfectTablePlan v6. Just wanted to make sure I wasn't missing anything.

    --
    Andy Brice
    https://www.perfecttableplan.com


  • Moderators

    @AndyBrice
    but this rather looks like the font size is different, than the weight.
    regular/normal specifies the weight of a font, not it's size.

    Is this a QTextDocument (QTextEdit etc) you are using here?



  • It is a QPainter::drawText() inside a QGraphicsItem derived object drawn on a QGraphicsCanvas.

    I believe it is the same font in both cases, but I will have a double check.

    --
    Andy Brice
    https://www.perfecttableplan.com



  • I am using pixel size, instead of point size. IIRC because I had issues with exporting point size fonts to PDF.

    On Qt 5.11.1:

    qDebug() << p->font();

    returns:

    QFont( "Arial,-1,144,5,50,1,0,0,0,0" )

    On Qt 4.8.5:

    qDebug() << p->font();

    returns:

    QFont( "Arial,-1,96,5,50,1,0,0,0,0" )

    They are both reading in the same font generated by QFont::toString(). So I am not sure why the pixel sizes are different. I need to dig further.

    --
    Andy Brice
    https://www.perfecttableplan.com



  • I investigated a bit further. 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" )
    

    So QFontInfo::pixelSize() seems to have changed between Qt 4 and Qt 5. It isn't a showstopper. But it is a minor annoyance.

    --
    Andy Brice
    https://www.perfecttableplan.com


  • Moderators

    @AndyBrice said in QFont::fromString() change between Qt 4 and Qt 5:

    QFont convertPointToPixelSize( const QFont& f )
    {
    QFont ret( f );
    QFontInfo fi( ret );
    ret.setPixelSize( fi.pixelSize() );
    return ret;
    }

    what is the purpose of this method in the first place?
    Do you want to set it's default pixel size?



  • @raven-worx IIRC it is because point font sizes don't work well when you export a QGraphicsCanvas to PDF. So I set pixel sizes (rather than point sizes) in the QGraphicsCanvas. To be honest, it is a bit lost in the mists of time and I am just trying to keep the Qt 5 version of my software backward compatible with the Qt 4 version.

    --
    Andy Brice
    https://www.perfecttableplan.com


  • Moderators

    @AndyBrice
    ok then in this case a workaround could be simply to calculate the point to pixel conversation yourself: 1 px = 0.75 pt
    It may be that the newer (Qt5) implementation takes the current screen resolution into account, where the older (Qt4) didn't.



  • @raven-worx Yes, it may be a related to the DPI of the device in Qt. I hadn't thought of that. In which case the ratio between pixels and points may vary between devices. I'll have to look through the QFontInfo::pixelSize() source. Thanks.

    --
    Andy Brice
    https://www.perfecttableplan.com



  • I have ended up multiplying the font point size by 0.66 when I read in plans created using Qt 4 on Windows. It is a bit of a hack, but it is good enough!

    --
    Andy Brice
    https://www.perfecttableplan.com


Log in to reply