Results 1 to 9 of 9

Thread: For Qt 4.6.x, how to auto-size text to fit in a specified width?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: For Qt 4.6.x, how to auto-size text to fit in a specified width?

    Hi there!

    Two days ago I solved it with QFont::setPointSizeF like this:

    Qt Code:
    1. float factor = rect().width() / painter->fontMetrics().width(name);
    2. if ((factor < 1) || (factor > 1.25))
    3. {
    4. QFont f = painter->font();
    5. f.setPointSizeF(f.pointSizeF()*factor);
    6. painter->setFont(f);
    7. }
    To copy to clipboard, switch view to plain text mode 
    This assumes, that your fonts width scales like its height. Works fine for me.

    HIH

    Johannes

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

    dchow (4th February 2010)

Similar Threads

  1. QVBoxLayout width size limit
    By QPlace in forum Qt Programming
    Replies: 7
    Last Post: 18th June 2009, 16:41
  2. Scale size of QGraphicsItem without scaling pen width
    By Lodorot in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2009, 00:18
  3. QLabel auto width?
    By DiamonDogX in forum Qt Programming
    Replies: 3
    Last Post: 14th April 2009, 07:25
  4. changing the size of the tab width: QTabWidget
    By nikita in forum Qt Programming
    Replies: 2
    Last Post: 29th August 2006, 08:31
  5. How to get size (length, width ....) of string or char
    By Krishnacins in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2006, 09:55

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.