c++qtqgraphicsitemqgraphicspixmapitem

Set transform origin point for an already transformed QGraphicsItem


I have a robotic hand which contains of two connected moving arms. when arm1 is being rotated, arm2 is also rotating around the same origin point of arm1. when arm2 is being rotated arm1 should not move and arm2 should rotate around arm2 origin point.

    QPixmap arm1(":/new/prefix2/pictures/arm1.png");
    QPixmap arm2(":/new/prefix2/pictures/arm2.png");

    QGraphicsPixmapItem *arm1p = new QGraphicsPixmapItem(arm1);
    QGraphicsPixmapItem *arm2p = new QGraphicsPixmapItem(arm2);

    QGraphicsScene *scene = new QGraphicsScene();

    arm1p->setScale(scale);
    arm1p->deviceTransform(ui->graphicsView->viewportTransform()).map(QPointF(0,0));

    arm2p->setScale(scale);
    arm2p->deviceTransform(ui->graphicsView->viewportTransform()).map(QPointF(0,0));

The following code for rotating arm1 and arm2 around arm1 origin point.

    arm1p->setTransformOriginPoint(29,375);
    arm1p->setPos(((29*scale)-29)+(390*scale),((375*scale)-375)+(106*scale));
    arm1p->setRotation(arm1angle);

    arm2p->setTransformOriginPoint(-581,478);
    arm2p->setPos(((-581*scale)-(-581))+(1004*scale),((478*scale)-478)+(0*scale));
    arm2p->setRotation(arm1angle);

And this works very well. But when I want to just rotate arm2, I want arm1 and arm2 to keep their positions and just rotate arm2 around different origin point. I wrote the following code but it's not working well.

    arm1p->setTransformOriginPoint(29,375);
    arm1p->setPos(((29*scale)-29)+(390*scale),((375*scale)-375)+(106*scale));
    arm1p->setRotation(arm1angle);

    arm2p->setTransformOriginPoint(-581,478);
    arm2p->setPos(((-581*scale)-(-581))+(1004*scale),((478*scale)-478)+(0*scale));
    arm2p->setRotation(arm1angle);

    arm2p->setTransformOriginPoint(97,148);
    arm2p->setRotation(arm2angle);

How can I let arm2 rotate around its origin point while keeping is rotation and position from arm1 rotation?


Solution

  • The problem is already solved simply by using setOffset and mapFromItem the final code is :

        QPointF c1 = arm1p->mapFromItem(bodyp,423,188);
        arm1p->setOffset(-29,-375);
        arm1p->setPos(c1.x()*scale,c1.y()*scale);
        arm1p->setRotation(arm1angle);
    
        QPointF c2 = arm2p->mapFromItem(arm1p,712-29,42-375);
        arm2p->setOffset(-97,-148);
        arm2p->setPos(c2.x()*scale,c2.y()*scale);
        arm2p->setRotation(arm1angle + arm2angle);