Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFont::fromString() change between Qt 4 and Qt 5
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 2 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AndyBriceA Offline
    AndyBriceA Offline
    AndyBrice
    wrote on last edited by
    #3

    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

    raven-worxR 1 Reply Last reply
    0
    • AndyBriceA AndyBrice

      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

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #4

      @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?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • AndyBriceA Offline
        AndyBriceA Offline
        AndyBrice
        wrote on last edited by
        #5

        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

        1 Reply Last reply
        0
        • AndyBriceA Offline
          AndyBriceA Offline
          AndyBrice
          wrote on last edited by
          #6

          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

          1 Reply Last reply
          0
          • AndyBriceA Offline
            AndyBriceA Offline
            AndyBrice
            wrote on last edited by
            #7

            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

            raven-worxR 1 Reply Last reply
            0
            • AndyBriceA AndyBrice

              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

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #8

              @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?

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              AndyBriceA 1 Reply Last reply
              0
              • raven-worxR raven-worx

                @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?

                AndyBriceA Offline
                AndyBriceA Offline
                AndyBrice
                wrote on last edited by
                #9

                @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

                raven-worxR 1 Reply Last reply
                0
                • AndyBriceA AndyBrice

                  @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

                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by raven-worx
                  #10

                  @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.

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  AndyBriceA 1 Reply Last reply
                  2
                  • raven-worxR raven-worx

                    @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.

                    AndyBriceA Offline
                    AndyBriceA Offline
                    AndyBrice
                    wrote on last edited by
                    #11

                    @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

                    1 Reply Last reply
                    0
                    • AndyBriceA Offline
                      AndyBriceA Offline
                      AndyBrice
                      wrote on last edited by
                      #12

                      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

                      1 Reply Last reply
                      2

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved