Results 1 to 6 of 6

Thread: adjust Font size to a given rect when drawText

  1. #1
    Join Date
    Oct 2007
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Red face adjust Font size to a given rect when drawText

    I have to adjust a Font size, when the user resized Widget where I draw text in a given rectangle.
    Can somebody to give me a hint or point me to the right example?
    Thanks in advance

  2. #2
    Join Date
    Sep 2007
    Posts
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    Hi,
    You should use the QFontMetrics class. This class has the boundingRect(QString text) method which is exactly what you want.

    The idea is to start with a initial font size (e.g. 12) and recusively check if your text can
    be drawn in your rect. If no, then you decrease the font size and check again!

    Panos

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

    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.

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

    yartov (14th May 2008)

  5. #4
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

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

    Quote Originally Posted by yartov View Post
    I have to adjust a Font size, when the user resized Widget where I draw text in a given rectangle.
    Can somebody to give me a hint or point me to the right example?
    Thanks in advance
    You can draw a arial 6point to a 2000 pixel QWidget and is not need to change point size
    only think Vector ...

    QPainterPath is your friend ...


    Qt Code:
    1. void ButtonAction::paintText()
    2. {
    3. QPainter *p = new QPainter(this);
    4. p->setRenderHint(QPainter::Antialiasing);
    5. const int borderI = 25;
    6. /* button text */
    7. QPainterPath textPath;
    8. textPath.addText(borderI,borderI * 2,fontText,textBut); //// fontText is any font() //// textBut text one line
    9. /* fix view from text */
    10. const QRectF reo = textPath.boundingRect();
    11. TXTrect = reo;
    12. /* tanslate text rect to rect window button */
    13. p->setWindow ( reo.x() , reo.y() , reo.width() , reo.height() );
    14. /* color text */
    15. p->setPen(Qt::NoPen);
    16. p->setBrush(colText);
    17. p->drawPath(textPath);
    18. Pm = p->deviceMatrix();
    19. p->end();
    20. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to patrik08 for this useful post:

    yartov (14th May 2008)

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

    Quote Originally Posted by yartov View Post
    I have to adjust a Font size, when the user resized Widget where I draw text in a given rectangle.
    Can somebody to give me a hint or point me to the right example?
    You can use QPainter::setWindow() and QPainter::setViewport() to modify the "scale" of the painter itself. Then you can use the same font size regardless of the real widget size, because you'll be drawing in logical and not physical coordinates.

  8. #6
    Join Date
    Oct 2007
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Windows

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

    Thanks, I implemented suggestions from other guys, but I want to try your suggestion too. Looks like the right one. Thanks a lot

Similar Threads

  1. Font size calculation when painting in a QImage
    By Ishark in forum Qt Programming
    Replies: 3
    Last Post: 15th July 2007, 23:22
  2. Replies: 3
    Last Post: 30th January 2007, 08:35
  3. QTreeView font size not being set
    By forrestfsu in forum Qt Tools
    Replies: 2
    Last Post: 12th October 2006, 14: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.