PDA

View Full Version : How to rotate items in a QGraphicsScene while keeping them upright



wayfaerer
1st February 2012, 06:05
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.

wysota
1st February 2012, 08:13
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.

stampede
1st February 2012, 16:28
If I understand you right, you simply want to rotate the line mid point:


QLine line = ...;
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).

wayfaerer
5th February 2012, 17:59
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.