View Full Version : Strange behaviour of painter->drawText

14th October 2015, 19:31

I ran into a rather strange problem.
I have a custom QGraphics Item, that should just print a Text.
The Paint Function looks like this:

void text::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
QPen temppen=brushes::textpen;
QFont tempfont=fonts::standardfont;

painter->drawText(boundingRect(),Qt::AlignCenter,getUiname( ));

But whenever this function is called the Text is shifted upwards, and are cut from the Top Border of the BoundingRect.

If I change the draw method to this:


The text is centered in the Bounding Rectangle as expected.

getUiname() is rather simple, I don't think that the problem is there.

QString uimodule::getUiname() const
return uiname;

The only thing left to mention is that getUiname is in the Baseclass of my GraphicsItem. But this should make no difference in my opinion.
Is this a bug? Any ideas what I could try to fix it?

14th October 2015, 20:52
The text returned by getUiName() is too wide for the rectangle you are passing to drawText() and is being wrapped to fit the width. Alternatively, the text has a newline char at at the end causing wrapping. The two lines no longer fit the vertical height so drawText() centres them, as requested, and truncates the top and bottom. If you provide drawText() with a pointer to a QRect it will tell you the rectangle you would need to fit the text.

14th October 2015, 21:18
Yes, the newline-char is causing the trouble.
When I cut the last Character of the String off, everything works fine.
Thank you!