# Rotate QRectF along center point

• Hello to all.

First of all thanks a lot for reading this post and being able to help.

I have a QRectF and now i would like to rotate it along its center point giving an angle.
is there any way to rotate it easyly or i have to calculate the new coordinates?

Thanks a lot!

• You'll probably want to use QTransform for this. Define the desired transformation, the use the "map" functions of QTransform.
Depending on what exactly you want, you can either user mapRect or work with a QPolygonF instead of a QRectF.

The easiest way is if the point of rotation is the origin (0/0). Otherwise, I have to first translate the point of rotation into the origin, then rotate, then translate in the inverse direction.

• @Asperamanca First of all thanks a lot for replying!
Coudl you show a little code please?

I am going to check QTransform!

Thanks again.

• @AlvaroS

``````qreal angle = ...;
QRect rect = ...;
Qpoint center = rect.center();
QTransform t = QTransform().translate( center.x(), center-y() ).rotate( angle ).translate( -center.x(), -center.y() );
QPolygon rotatedRect =  t.mapToPolygon( rect );  // mapRect() returns the bounding rect of the rotated rect
``````

alternatively you can transform each point of the rect individually.

• @raven-worx Thanks a lot for replying!

Is there any way to convert from QPolygon to QRectF? Because I would like to add to a scene the rectange as QGprahicsRectItem.

Thanks a lot!

• @raven-worx I have just done your code and it does not work. Rect translate but nor rotate!

• @AlvaroS said:

@raven-worx Thanks a lot for replying!

Is there any way to convert from QPolygon to QRectF? Because I would like to add to a scene the rectange as QGprahicsRectItem.

Thanks a lot!

Well, that changes a lot. Because in this case it is better to rotate the QGraphicsRectItem within the scene.

Use setTransformOriginPoint to specify the point where the rect should rotate around.
The use setRotation to rotate it.

• @Asperamanca Thanks a lot!
It works but not at all.
My code is:

``````boxes_rotated->setTransformOriginPoint(center_box);
boxes_rotated->setRotation(-ui->Degrees_to_Rotate->text().toDouble());
``````

I mean, in scene Rectangle rotate really good but the coordinates points rectangle do not change... so they are the same rotated or not rotated. How can I fix that?

thanks a lot!

• @AlvaroS said:

Is there any way to convert from QPolygon to QRectF?

no, since it has a reason why a QPolygon is returned and not a QRect. A transformed rect isn't a "normal" rect anymore. In your case it still may arguable,. But imagine a transformation which is also shearing for example. Then a rect becomes an arbitrary shape.

Because I would like to add to a scene the rectange as QGprahicsRectItem.

why not adding the rect item and apply the transformation directly to it?

• @raven-worx

``````            QRectF newbox = current_box->rect();

QPointF center_box = current_box->rect().center();

std::cout<<"before: "<<newbox.topLeft().x()<<std::endl;
current_box->setTransformOriginPoint(center_box);
current_box->setRotation(-ui->Degrees_to_Rotate->text().toDouble());
std::cout<<"after: "<<boxes_rotated->rect().topLeft().x()<<std::endl;
``````

current_box is a QGraphicsRectItem.

This box is which I want to rotate. So in the scene it is rotated good. But its points are still the same...

• @AlvaroS said:

This box is which I want to rotate. So in the scene it is rotated good. But its points are still the same...

because this only alters it's trasnformation. The item's rect is unchanged.
If you really want to change the coordinates of the item and display a rotated rect item you only can only use a polygon.

note: in a "normal" rect (per definition) the top-left and top-right coordinates have the same y-value, the top-right and the bottom-right the same x-value, and so on...

• @raven-worx Thanks a lot!!

I have use what you said in your previous post like this:

``````            QRectF newbox = boxes_rotated->rect();

QTransform t = QTransform().translate( center_box.x(), center_box.y() ).rotate( -ui->Degrees_to_Rotate->text().toDouble() ).translate( -center_box.x(), -center_box.y() );
QPolygon rotatedRect =  t.mapToPolygon( newbox.toRect() );  // mapRect() returns the bounding rect of the rotated rect

``````

but the problem is that in t.mapToPolygon I have to use QRect and QPolygon instead of QRectF and QPolygonF and I loose precision.

Is there any way to do that without loosing precision?!

thanks a lot again

• @AlvaroS said:

Is there any way to do that without loosing precision?!

Use one of the map() overloads
QPolygonF accepts a QRectF via constructor for example.