View Full Version : Best/Fast approach to draw text in any given rectangle

23rd June 2009, 10:37

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?)

QString text="test text";
QFont f=painter->font();
QFontMetrics fm=painter->fontMetrics();
//increase font
//similar loops for greater width and height

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....:mad:

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.

23rd June 2009, 11:05
How about using QPainter::setWindow() and QPainter::setViewport()?

23rd June 2009, 11:21
How about using QPainter::setWindow() and QPainter::setViewport()?

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.

23rd June 2009, 13:52
anyone anymore ideas?

23rd June 2009, 14:10
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 (http://doc.trolltech.com/qq/qq24-textlayouts.html) :)

23rd June 2009, 15:45
You mean Low-Level Text Layouts but it doesn't do scaling.

24th June 2009, 03:03
thx for your input aamer bhai... but as wysota said.. my main problem is fast scaling.

24th June 2009, 09:28
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).

24th June 2009, 09:56
looks like i have to code a little bit of everything.. :(. Thx wysota jee.

24th June 2009, 13:14
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.

25th June 2009, 03:09
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?:D