Issue with rotating a QGraphicsRectItem

  • Hi All,

    I am facing an issue with the incremental rotation of QGraphicsRectItem.

    I have added a QGraphicsRectItem in the scene and rotated it using the setTransform API. ( say by angle = 60 degree )

    Now I want to update the item with 90 degree rotation. So the steps which I followed are,

    1. setRect( originPt, width, height )
    2. setTransform( 90 degree )

    Now I did this, based on the assumption that setRect will update the exisitng rectangle with origin point, width, height and 0 degree. And then calling a setTransform with 90 degree shall fix the job. But the setRect is not updating the angle part of the item, which is existing and as a result, it is drawing an additional 90 degree on the already rotated item. I find this quite confusing.

    Could any one kindly help with providing more information on the setRect API ?

    Thanks in advance,

  • Lifetime Qt Champion


    setRect does exactly what is described: it sets the item's rectangle. It doesn't modify the transformation you did.

    How exactly are you applying the transformation ?

  • @SGaist

                    QLineF diagonal( 0 ), 2 ) );
                    QPointF rotatedCenter( 0 ).x() + ( diagonal.dx()/2 ),
                                  0 ).y() + ( diagonal.dy()/2 ) );
                    QLineF yaxis( 0, 0, 0, -10 );
                    QLineF angleLine( 3 ), 0 ) );
                    qreal angie = yaxis.angleTo( angleLine );
                    box->setAngle( angie );
                    qreal angleValue = -1 * box->angle();
                    QPointF center = box->boundingRect().center();
                    QTransform transform;
                    QPointF pt = rotatedCenter - center;
                    transform.translate( pt.x(), pt.y() );
                    transform.translate( center.x(), center.y());
                    transform.rotate( angleValue );
                    transform.translate(-center.x(), -center.y());
                    box->setTransform( transform, true);

  • Hi @SGaist ,
    The issue I am having is that, I have the absolute angle value. So everytime, I have to reset the rotation and apply the transformation with the absolute angle.

  • Lifetime Qt Champion

    Why not calculate the delta of the angle and transform from that ?

  • @SGaist It makes my computation little more complicated. However, it seems like the most viable option.
    Thanks for the suggestion.

Log in to reply