[Qt-interest] QFontMetrics question

Nikos Chantziaras realnc at arcor.de
Sat May 8 01:29:00 CEST 2010


On 05/07/2010 01:21 PM, amulya rattan wrote:
> Hi there,
>
> A query regarding QFontMetrics and which api to use for calculating size
> of a QString. The size is then used to create an svg out of text string.
> However I am not sure whether to go with width() or boundingRect(). In
> the attached example, I have used both functions, once with whole string
> and once finding whole width as sum of widths of characters in the
> string. Using width(), both times(just string and sum of chars in
> string), the result was same(206). However, using boundingRect(), the
> results are different:
>
> 1. with whole string : 206(similar to results with width())
> 2. as sum of char width in string : 150!
>
> Can someone tell me why such a difference is there and which api should
> i use if i want to create an svg out of this string?
> Appreciate any response.

Don't assume the sum of width() of all characters is equal to the 
width() of the whole string.  This is because of kerning.  Look at this 
string: "Yo".  In non-monospaced fonts, you will see that the "o" is 
actually under the "Y".  That means the total width of "Yo" is smaller.

Which one to use depends on what you want to do.  If you need to know 
the distance between the start of the string/character and the point 
where you should start drawing the next string/character, use width(). 
If you just need to know the real space taken by the string on-screen, 
use boundingRect(), but in that case you don't know where to start 
drawing the next string (and if you do anyway, the spacing will be wrong.)



More information about the Qt-interest-old mailing list