Unsolved Rotate QRectF along center point
-
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?
-
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 scene->addPolygon(rotatedRect,blue);
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.