PDA

View Full Version : Rotate a QRectF



whitefurrows
14th July 2009, 15:28
Hi,

i try to rotate a QRectF like this:

QPainter painter(this);
painter.rotate(270);
QTextOption textOption(Qt::AlignCenter);
painter.drawText( QRectF(50,50,100,20);, "Hello World", textOption );

but that code don't working. I would be rotate the QRectF at the QRectF center. How can i do that?

Lykurg
14th July 2009, 15:49
have a look at QMatrix and QTransform. (->translate)

whitefurrows
15th July 2009, 09:31
Now i have try to rotate the QRectF like this:

QRectF rect(50,50,100,20);
QTransform transform;
transform.rotate(270,Qt::XAxis);
transform.translate(rect.width(), rect.height());
QRectF transformed = transform.mapRect(rect);

QTextOption textOption(Qt::AlignCenter);
painter.drawText( transformed, "Hello World", textOption );

but rotating with QTransform don't working too. What am i doing wrong?

caduel
15th July 2009, 09:36
you need to use translate() to put the rotation center inside your rect's center and then use QRect(-50,-10,100,20)

whitefurrows
15th July 2009, 10:53
QRectF(-50,-10,100,20) center the rect and QTransform::translate ( qreal dx, qreal dy ) is the x and y axis to paint the rect? My rec is not rotated and paint on the given x-coordinate and y-coordinate. What am I doing wrong again?

whitefurrows
15th July 2009, 15:33
It's not necessary to use QMatrix and QTransform to rotate a QRectF. Important is to translate the painter before rotate and center the rect like the following example:


QPainter painter(this);
painter.translate(50, 50);
painter.rotate(270);

QRectF rect(-50,-10,100,20);
QTextOption textOption(Qt::AlignCenter);
painter.drawText( rect, "Hello World", textOption );

Thanks for your help!