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?)
Code:
while(fm.width(text)<this->width())
{
//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.
Re: Best/Fast approach to draw text in any given rectangle
Re: Best/Fast approach to draw text in any given rectangle
Quote:
Originally Posted by
wysota
Thx for the input. So by hyperlinking through your given links... i read this..
Quote:
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.
Re: Best/Fast approach to draw text in any given rectangle
Re: Best/Fast approach to draw text in any given rectangle
Quote:
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 :)
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.
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.
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).
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.
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.
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?:D