Results 1 to 11 of 11

Thread: Best/Fast approach to draw text in any given rectangle

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Red face Best/Fast approach to draw text in any given rectangle

    Hi,

    I will be drawing a lot of text on a widget which user can resize to any size.
    The text should grow or shrink according to the widget size.

    One simple solution is to set normal font size on qpainter first and then in a loop increase or decrease the font size by 2 points(or 1?)

    Qt Code:
    1. QString text="test text";
    2. QFont f=painter->font();
    3. QFontMetrics fm=painter->fontMetrics();
    4. while(fm.width(text)<this->width())
    5. {
    6. //increase font
    7. }
    8. //similar loops for greater width and height
    To copy to clipboard, switch view to plain text mode 

    i have to draw several lines of text, in irregular shapes (triangles, diamonds, squares, etc). And also need to draw Tables. So as u can assume that the above litter code for determining the size will become complicated.

    i just wanna know if anybody done this before in a more elegant way??

    recently there is a QPainter::scale() function... but ppl say it cant render the font properly at all sizes....

    PS: the "text" to draw is dynamically changing every now and then.. so i cant optimize just by calculating the size just once in every resizeEvent only.
    Last edited by nish; 23rd June 2009 at 10:00. Reason: updated contents

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. How to draw vertical text via QTextEdit
    By nifei in forum Qt Programming
    Replies: 8
    Last Post: 1st October 2010, 17:10
  3. Html text bounding rectangle
    By bunjee in forum Qt Programming
    Replies: 8
    Last Post: 15th May 2008, 11:25
  4. Writing Text on a Rectangle!!!
    By Kapil in forum Qt Programming
    Replies: 3
    Last Post: 17th May 2006, 10:23
  5. Draw a rectangle alternating two colors with qPainter
    By SkripT in forum Qt Programming
    Replies: 12
    Last Post: 24th January 2006, 23:12

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.