Results 1 to 7 of 7

Thread: Find maximum size of a given font that will fit a given size

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: Find maximum size of a given font that will fit a given size

    Once I had the same problem, but I don't have found a build in function. Another solution could be that you paint the text with a default size (with a few checks if you want the text to be drawn in 1, 2, 3, ... lines) and then simply scale the image. If you can live with the lower quality that could be also an option. I had chosen the solution with finding the best font size.

  2. #2
    Join Date
    Jul 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Find maximum size of a given font that will fit a given size

    OK thanks, good to know someone has been in the same boat as me in the past.

  3. #3
    Join Date
    Mar 2007
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Find maximum size of a given font that will fit a given size

    My requirements are a bit different, but here's my code.

    Qt Code:
    1. void
    2. TextItem::setGeometry(const QRectF& rect)
    3. {
    4. QFont theFont = font();
    5.  
    6. int size = qMin(static_cast<int>(rect.height()), 40);
    7.  
    8. theFont.setPixelSize(size);
    9. QFontMetricsF fm(theFont);
    10. while(fm.width(toPlainText()) > rect.width())
    11. {
    12. size -= 5;
    13. theFont.setPixelSize(size);
    14. fm = QFontMetricsF(theFont);
    15. }
    16.  
    17. setFont(theFont);
    18. setPos(rect.topLeft());
    19. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. The maximum size of a QList
    By Denarius in forum Newbie
    Replies: 3
    Last Post: 30th November 2012, 10:27
  2. adjust font size to QLabel-size
    By hunsrus in forum Qt Programming
    Replies: 0
    Last Post: 9th July 2008, 14:33
  3. maximum size of xml files
    By freeskydiver in forum Qt Programming
    Replies: 1
    Last Post: 15th January 2008, 10:02
  4. change font size and button size of QMessageBox
    By nass in forum Qt Programming
    Replies: 6
    Last Post: 13th September 2006, 19:16
  5. howto expand frame to maximum size
    By masoroso in forum Newbie
    Replies: 4
    Last Post: 21st April 2006, 09:40

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.