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. #7
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 90 Times in 88 Posts

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

    I needed this functionality myself - here is what I came up with:

    Qt Code:
    1. qreal lod = option->levelOfDetailFromTransform(painter->worldTransform());
    2. QRectF r = boundingRect();
    3. QFont f = painter->font();
    4. qreal aspectRatio = painter->fontMetrics().lineSpacing() / painter->fontMetrics().averageCharWidth();
    5. int pixelsize = sqrt(r.width()*r.height()/aspectRatio/(stitle.length()*3))*aspectRatio;
    6. f.setPixelSize(pixelsize);
    7. int flags = Qt::AlignCenter|Qt::TextDontClip|Qt::TextWordWrap;
    8. if ((pixelsize * lod) < 13)
    9. flags |= Qt::TextWrapAnywhere;
    10. QRectF tbr = fm.boundingRect(r,flags,stitle);
    11. pixelsize = f.pixelSize()*qMin(r.width()*0.95/tbr.width(),r.height()*0.95/tbr.height());
    12. f.setPixelSize(pixelsize);
    13. painter->setFont(f);
    14. painter->drawText(r,flags,stitle);
    To copy to clipboard, switch view to plain text mode 
    The idea of the first attempt to calculate a pixelsize is to calculate how big a letter can be if you want to distribute a certain amount of letters on the available space.

    numberOfLetters = width / charWidth * height / (charWidth*aspectRatio)

    => pixelSize = charWidth * aspectRatio = sqrt ( width * height / numberOfLetters / aspectRatio) * aspectRatio;

    For my scenario numberOfLetters is 3 times the amount of text I want to display, because I don't want to cover the whole item with text.

    With this guess I call QFontMetricF::boundingRect and fine tune the result accordingly.

    This works fine for my purpose.

    HIH

    Johannes
    Last edited by JohannesMunk; 5th February 2011 at 14:42.

Similar Threads

  1. QVBoxLayout width size limit
    By QPlace in forum Qt Programming
    Replies: 7
    Last Post: 18th June 2009, 17:41
  2. Scale size of QGraphicsItem without scaling pen width
    By Lodorot in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2009, 01:18
  3. QLabel auto width?
    By DiamonDogX in forum Qt Programming
    Replies: 3
    Last Post: 14th April 2009, 08:25
  4. changing the size of the tab width: QTabWidget
    By nikita in forum Qt Programming
    Replies: 2
    Last Post: 29th August 2006, 09: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, 10: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.