Results 1 to 6 of 6

Thread: adjust Font size to a given rect when drawText

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Join Date
    Jun 2007
    Posts
    56
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: adjust Font size to a given rect when drawText

    You could also do something like this...

    Qt Code:
    1. QFont serifont( "Times" );
    2. qreal size1_x, size2_x, size1_y, size2_y;
    3. QMatrix xform = painter.combinedMatrix();
    4. QMatrix invertedxform = xform.inverted();
    5.  
    6. // calculate and set pixel height for font to be 3% of the display area size
    7. invertedxform.map( qreal( width() * 0.10 ), qreal( height() * 0.10 ), &size1_x, &size1_y );
    8. invertedxform.map( qreal( width() * 0.13 ), qreal( height() * 0.13 ), &size2_x, &size2_y );
    9. serifFont.setPixelSize( static_cast< int >( qAbs( size2_x - size1_x ) ) );
    10. painter.setFont( serifFont );
    To copy to clipboard, switch view to plain text mode 

    This will ensure that the font size will stay the same if you scale the coordinates of the widget. I'm sure there's a more elegant way to accomplish it though.

  2. The following user says thank you to Micawber for this useful post:

    yartov (14th May 2008)

Similar Threads

  1. Font size calculation when painting in a QImage
    By Ishark in forum Qt Programming
    Replies: 3
    Last Post: 15th July 2007, 22:22
  2. Replies: 3
    Last Post: 30th January 2007, 07:35
  3. QTreeView font size not being set
    By forrestfsu in forum Qt Tools
    Replies: 2
    Last Post: 12th October 2006, 13:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.