View Full Version : how to get the boundingrect of multi line text using qfontmetrics

22nd April 2014, 15:31
hi friends,

I want to know how i can get the boundingRect of the multi line text from the QFontMetrics.
im having a qgraphicsItem where i have to draw text .
while drawing im using the fontmetrics bounding rect to define the rect of the text to draw .

QFontMetrics fontMtic(segoeFont);
m_bounds = fontMtic.boundingRect(theCurrentText);
p->drawText(QRectF(theTextItemPos, m_bounds.size()),Qt::TextWordWrap, theCurrentText);

but when the theCurrentText is coming with multi line or having new line in it , the bounding rect value of the fontmetric return me the height of the first line only .

I want to get the complete boundary height for the text.

can any one please help me to solve this problem .

22nd April 2014, 16:58
Since your drawText() call is one of the overloads that takes flags,you need to use one of the boundingRect() calls that uses the same flags.

Alternatively, there is a drawText() overload that takes an options QRect* as its last argument. If you pass in a pointer to a local QRect variable, e.g. "&m_bounds", it will set that variable to the bounding rect actually used for drawing the text.