How to rotate items in a QGraphicsScene while keeping them upright
Hello, I'd like to be able to rotate some horizontal lines about the midpoint of a QGraphicsScene, but keep the lines themselves horizontal. Is there a simple way to do this? My only idea is to create a QTransform object and call QTransform::rotate, then use QTransform::map to map one of every line's two coordinates, then translate each line individually using QGraphicsItem::translate. But I can't figure out how I'd actually get each line's initial coordinate (for putting into QTransform::map), or if there is a more elegant and efficient way of doing it. Speed is a bit of a concern because there will be about one or two hundred lines, and the rotation will be continuous.
I'd appreciate any advice.
Re: How to rotate items in a QGraphicsScene while keeping them upright
Could you provide a clearer explanation of what you want to achieve? As now it seems you want to rotate things without them rotating which doesn't make much sense.
Re: How to rotate items in a QGraphicsScene while keeping them upright
If I understand you right, you simply want to rotate the line mid point:
Code:
QTransform t;
t.rotate(angle); // actually just figure it out yourself how to rotate around specified point
const QPoint mid
= (line.
p1()+line.
p2)/2;
const QPoint rotated_mid
= t.
map(mid
);
line.translate( rotated_mid-mid );
You cannot rotate the start and end points separately, it will change the line direction (if angle != 0).
Re: How to rotate items in a QGraphicsScene while keeping them upright
Thank you! That worked, although I actually wound up finding a simpler solution for my specific case. The lines are on top of QGraphicsItems that *do* rotate normally. So all I had to do was set each line's parent as its corresponding QGraphicsItem, then set the ItemIgnoresTransformations flag on the lines. I think I'm going to wind up using your approach elsewhere, though. Thanks again.