Results 1 to 9 of 9

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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?

    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 13:42.

  2. #2
    Join Date
    Jul 2010
    Posts
    37
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

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

    Quote Originally Posted by JohannesMunk View Post
    This works fine for my purpose.
    I'm confirming you that this solution (i named it the 2nd) fit at the best my requirements too.
    Here is my two solutions comparison:
    solutioncomparison.jpg

    My scene doesn't has too much items, so the drawing performances are acceptable.
    Thank you guys for shared your work.

  3. #3
    Join Date
    Mar 2016
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

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

    Hi Johannes / All

    Could you please explain the math behind this?

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

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

    I don't follow it.

    Thank you

    Tim

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.