Moving the rotated Rect back to original center after QPainter::rotate()



  • qreal anyDegree=20;
    Rect myRect(0,0,100,100); //original center is (50, 50)
    QPainter painter;
    painter.drawRect(myRect);
    painter.rotate(anyDegree); //need to find the new center of this rotated shape?
    

    I'd like to move the rotated shape back to it's original location (50,50). I'm thinking to calculate the new center of rotated rect with QtMath cos and sin etc, but that seems pretty nasty... I wonder if there is a more elegant way to find the new center of a rotated Rect after QPainter::rotate()? Or is there a better way to move rect back after such rotation? Thank you very much.



  • Hi,
    do you mean you want to rotate around the original center? To rotate around (any) given point you just have to translate to that point, rotate there and then translate back again. Something like this:

    painter.translate(center);
    painter.rotate(anyDegree);
    painter.translate(-center);
    

    -Michael.



  • @m.sue

    This is great. Translate it to (0,0) for the rotation then translate back to original center... smart!



  • @Pauly

    Actually this does not quite work if I have two shapes... What should I do if I just want to rotate one but not the other? It seems that painter.translate and rotate affects all shapes drawn on it... Thanks.



  • @Pauly
    you can use save and restore

    take a look at analog clock example,it teach you how to use QPainter


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.