PDA

View Full Version : Drawing Text



QbelcorT
7th March 2009, 07:21
Hi,
I have QtEmbedded 4.4.1. I am trying to draw text within a rectangle (as per the picture), however, there is too much spacing between the lines. I know this is probably related to the QFontMetrics::lineSpacing().
I have a button rectangle, and a text rectangle, I am drawing the text rectangle inside the button rectangle. The text rectangle coordinates are relative to the button coordinates, not the screen coordinates, that is why I have to draw an adjusted button rectangle.
Anyways, this works, but the font display spacing is too large, and I must also consider different translations ( accents on the chars, etc).
image.png is actual
spec.png is required



QFontMetrics fm(txtFont);
qDebug() << fm.lineSpacing() << fm.leading() << fm.height();
QRectF adjRect(rect().adjusted(txtRect.x(),txtRect.y(),
-txtRect.x(), -(rect().bottom() - (rect().y()+txtRect.y()+txtRect.height()))));
painter->drawText(adjRect,Qt::AlignCenter|Qt::TextWordWrap, messageLabel);
painter->drawRect(adjRect); // for debugging


1) Can I reduce the line spacing? (I would like to increase the font size)
2) Is this the best method to draw text taking into consideration other languages? I worry that the top line will cut-off accents.

wysota
7th March 2009, 09:01
1) Can I reduce the line spacing? (I would like to increase the font size)
I don't think you can. Unless you use another font with a smaller leading value.


2) Is this the best method to draw text taking into consideration other languages? I worry that the top line will cut-off accents.

It's best to ask the font metrics for the geometry of a text you want drawn. QFontMetrics::boundingRect() will help you do that.

QbelcorT
7th March 2009, 09:34
thanks for the advice.
I am using "Times" or "Helvetica". The leading value is always 0 or in some cases -1, it's the linespacing that changes. I thought I would include this if it gives you another idea.

For example
Times @ font size 15, line spacing = 17
Times @ font size 16, line spacing = 23
Helvetica @ font size 17, line spacing = 23
Helvetica @ font size 16, line spacing = 22

edit: I guess using BoundingRect will be difficult if I am word wrapping..? :confused:
sorry, edit again.. I tried BoundingRect, seems ok regarding the wordwrapping.. still don't like the linespacing.

wysota
7th March 2009, 12:19
Line spacing = leading + height. Note that "height" is not the point size of the font but rather ascent+descent+1.

QbelcorT
8th March 2009, 07:16
Hi,
I made an error yesterday when drawing the text.I forgot the draw the text in the font bounding rect.
I had this, and it was working even when the text was too long. However this is incorrect since it isn't using the boundRectText.


painter->drawText(adjRect,Qt::AlignCenter|Qt::TextWordWrap, messageLabel);


I changed the line to this and now the bounding rect expands whenever the text is expanded. It doesn't constrain to the adjRect.


painter->drawText(boundRectText,Qt::AlignCenter|Qt::TextWor dWrap,messageLabel);



Here is my code.


QFontMetrics fm(txtFont);
QRectF adjRect(rect().adjusted(txtRect.x(),txtRect.y(),-txtRect.x(), -(rect().bottom() - (rect().y()+txtRect.y()+txtRect.height()))));
QRect boundRectText(fm.boundingRect(adjRect.toRect(),Qt: :AlignCenter|Qt::TextWordWrap,messageLabel));
painter->drawText(boundRectText,Qt::AlignCenter|Qt::TextWor dWrap,messageLabel);
painter->drawRect(adjRect); // debugging
painter->drawRect(boundRectText); // debugging


Here is a debug of the size of rectangles
QRect(2,127 473x40) = adjRect = good
QRect(-190,122 857x50) = boundRectText = bad. I expected this to be constrained to adjRect.
Is there something I am doing wrong?
Thanks

wysota
8th March 2009, 11:37
If the text doesn't fit, the rect you pass will not be respected. You have to pass a rectangle capable of holding the text or larger. That's why we have QFontMetrics::boundingRect in the first place. It makes it possible to adjust the font if you see the text won't fit into the desired area.

QbelcorT
8th March 2009, 13:07
OK thanks, I understand, I need to put a restriction on the text length for translations. I would rather have the text cut-off then overwrite the button.

wysota
8th March 2009, 17:33
You can elide the text if it is too long, that's what is usually done in such cases.

By the way, you can probably solve your problems by using QTextLayout instead of QPainter::drawText(). It's more difficult in use but gives you more control over the layout (line spacing included).