Re: Center text on painter
Use QFontMetrics to find the height, width, ascent and descent of the text you use.
Re: Center text on painter
Quote:
Originally Posted by masoroso
I now use something like p.drawText(x,y,`bla') but there must be a way to find out how large 'bla' will be in pixels?
Either use QPainter::boundingRect() or paint the text using different QPainter::drawText() method (the one which takes QRect as the first parameter).
Re: Center text on painter
thanks!
I now use
Code:
...
tw = fm.width(sx);
painter.drawText(x+tw/2, y, sx);
btw. I did not understand the boundingRect option... I still need to know the rect dimensions before I call `boundingrect'??
Anyway, it works this way so thank you both,
Rob
Re: Center text on painter
whoops sorry..
boundingrect is a better option in this case because QFontMetrics does not have a height() option (and I also need to align to the center of the height).
The code is now rewritten to
Code:
..
xoffset = fm.boundingRect(sx).width()/2;
painter.drawText(x - xoffset, y, sx);
Re: Center text on painter
Quote:
Originally Posted by masoroso
I still need to know the rect dimensions before I call `boundingrect'??
No, you don't have to:
Quote:
If the text does not fit within the given rectangle using the specified flags, the function returns the required rectangle.