Results 1 to 11 of 11

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

  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 11:00. Reason: updated contents

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    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

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

    Quote Originally Posted by wysota View Post
    Thx for the input. So by hyperlinking through your given links... i read this..

    One frequent need for the transformation matrix is when reusing the same drawing code on a variety of paint devices. Without transformations, the results are tightly bound to the resolution of the paint device. Printers have high resolution, e.g. 600 dots per inch, whereas screens often have between 72 and 100 dots per inch.
    I should read more QtAssistant. But at the first look it seems like i have to play a lot with qpainter.

  4. #4
    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

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

    anyone anymore ideas?

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    i have to draw several lines of text, in irregular shapes (triangles, diamonds, squares, etc).
    There was some code in Qt Quarterlys... which had text inside iregular shapes like balloon.. search might be fruitful I cudnt find the link yet...

    Found it.. here

  6. The following user says thank you to aamer4yu for this useful post:

    nish (25th June 2009)

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

    You mean [wiki]Low-Level Text Layouts[/wiki] but it doesn't do scaling.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    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

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

    thx for your input aamer bhai... but as wysota said.. my main problem is fast scaling.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

    You can calculate the font size by approximations but it will still be an iterative approach (although you would probably have to make 3-4 iterations at most).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    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

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

    looks like i have to code a little bit of everything.. . Thx wysota jee.

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

    Try approximating - first make a rough approximation (based on the difference of widths between font sizes), then make a smaller one, when you go over the border than change directions, etc. It should be quite fast.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. The following user says thank you to wysota for this useful post:

    nish (25th June 2009)

  13. #11
    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

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

    ya,, thx... thats a good idea... This will also be usefull when the application is using other language font.


    EDIT: This site should be renamed to wysota center... btw how do u pronounce it?
    Last edited by nish; 25th June 2009 at 03:16. Reason: updated contents

Similar Threads

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