[Qt-interest] QFontMetrics boundingRect() vs width()
Joshua Grauman
jnfo-c at grauman.com
Fri Jun 18 06:11:52 CEST 2010
Thanks for this explanation. This makes good sense.
Going back to your example of drawing "Yo" by drawing "Y" first and then
"o". Is there any way to figure out what the QFontMetrics::boundingRect()
would be just from individual functions on "Y" and "o"? In other words, is
there a way to see what effect the kerning has had on the overal
boundingRect?
Thanks!
Josh
> On 06/18/2010 04:27 AM, Josh wrote:
>> Hello all,
>>
>> I'm working on some things with text layout and need to better
>> understand how text is laid out. Obviously,
>> QFontMetrics::boundingRect().width() is different than
>> QFontMetrics::width(). What I'm trying to figure out is how they are
>> related. For example, if I have a string, "This is a sentence" what
>> causes the two values to be different? I thought it would be related to
>> QFontMetrics::leftBearing() and rightBearing(), but I can't see any
>> relationship there. Is there any way to figure out what the bounding
>> rect will be based on the width? Any information about the relationship
>> between these will be appreciated! Thanks!
>
> QFontMetrics::width() gives the position at which subsequent text should
> be drawn. FontMetrics::boundingRect() gives the real bounding rectangle
> of the text. QFontMetrics::width() is usually smaller because of font
> kerning. Kerning is the reason QFontMetrics::width() is needed at all.
>
> So if you have a piece of text than you're drawing, and need to know
> where to begin drawing subsequent text, use QFontMetrics::width(). To
> give an example, imagine you're trying to draw two words, "foo" and
> "bar", one after another to form the final word "foobar". You draw
> "foo" first at position X, and then draw "bar" at position X +
> QFontMetrics::width("foo"). You cannot do this with
> QFontMetrics::boundingRect().width("foo"). That would give you the real
> width of "foo" rather than the width offset at which to draw the next text.
>
> You can experiment with this and see for yourself. I recommend drawing
> the string "Yo", in two steps: "Y" first and then "o". You'll see the
> effect of kerning and why QFontMetrics::boundingRect() is not suitable
> and will result in ugly spaced text rendering.
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
More information about the Qt-interest-old
mailing list