Important: Please read the Qt Code of Conduct -

Change in the geometry of item on changing parent

  • Say I have an item A which is child of a parent P1.
    Now I rotate item A by 180.
    I have another item P2 which is rotated by 90 and horizontally mirrored.
    Now when I try to make P2 as parent of item A, it's position, rotation and mirroring gets completely modified.

    I am able to fix the position of item by adjusting it based on its scene position.
    But I am unable to get its original transformations (rotate/mirror) back.
    How can I resolve this??
    Also is there any way to modify the scene transformation of any item ??

  • Lifetime Qt Champion


    Can you share the related code ?

  • /* Below I am sharing a minimal stand alone code simulating the problem shared above, its not the actual code from my project */

    QGraphicsView graphicsView;
    QGraphicsScene *scene = new QGraphicsScene(&graphicsView);

    QGraphicsRectItem * parent1 = new QGraphicsRectItem();
    parent1 ->setRect(QRectF(-100, -50, 200, 100);
    parent1->setPos(100, 100);
    scene->addItem(parent1 );

    QGraphicsRectItem * parent2 = new QGraphicsRectItem();
    parent2 ->setRect(QRectF(-100, -50, 200, 100);
    parent2->setPos(250, 250);
    scene->addItem(parent2 );

    //parent1 being set as parent of text
    QGraphicsSimpleTextItem * text = new QGraphicsSimpleTextItem();
    text->setText("I am Text");

    //rotate parent2 by 90 and then make it as parent of text
    QTransform tr;

    Now text will jump to another position along with some rotation applied (inherited from parent).
    I am to handle to jump in position as below:

    QPointF oldPos = text->mapToScene(pos()); //fetch scene pos before setting parent2
    QPointF newPos = text->maptoScene(pos()); // fetch pos after setting parent2

    text->setPos( pos() + (oldPos - newPos) );

    Now text stays where it was created but rotates.
    How to stop rotation?
    I know there is a flag itemIgnoresTransformation.
    In our case there is a hierarchy of parents and some view transformations like zoom which we want to get automatically passed on to all the child items.
    So itemIgnoresTransformation is not the solution.

  • Anybody having any expertise around this problem, please share.

  • Lifetime Qt Champion

    From the looks of it, you would have to implement your own filtering of the transformations applied to your scene but since it's all handled on a lower level.

    See the QGraphicsSceneBspTreeIndex.

Log in to reply