Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsItem parent transformation
Qt 6.11 is out! See what's new in the release blog

QGraphicsItem parent transformation

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 818 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sDmt
    wrote on last edited by
    #1

    Hi,
    I have two QGraphicsItem objects as in the image
    Capture.JPG
    What I need is:

    • Make the ChildObject moves as I drag the parent which was easily done by setParentItem to ParentObject.

    • I want to be able to rotate the ParentObject while keeping the ChildObject fixed.

    The best I could come up with was setting the parent of the childobject to nullptr when rotating and setting back to ParenObject when moving the ParentObject

    QVariant MyShape::itemChange(GraphicsItemChange change, const QVariant &value)
    {
     if(change == QGraphicsItem::ItemPositionChange)
           childobject->setParentItem(this); 
     else if (change == QGraphicsItem::ItemRotationChange)
        {
            childobject->setParentItem(nullptr);
            scene()->update();
        }
    }
    

    I have initialized the ChildObject in the constructor of the ParentObject shape to appear under the ParentObject

    ChildObject *childobj = new ChildObject;
    
        QPointF mPos;
        mPos.setX(rect().bottomLeft().x() + rect().width()/2 -
                      childObject->boundingRect().width()/2);
    
        mPos.setY(rect().bottomLeft().y() + rect().height()/3);
        childobj ->setPos(mapFromItem(this,mPos));
        childobj->setFlag(QGraphicsItem::ItemIgnoresTransformations,true);
        childobj->setParentItem(this);
    

    However, when I run the code, the ChildObject moves when I rotate the ParentObject. I don't know what I am missing, I think its about the coordinate of the Parent/Scene issue but not sure. Please help.

    Thanks in advance

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Based on the flag description:

      The item ignores inherited transformations (i.e., its position is still anchored to its parent, but the parent or view rotation, zoom or shear transformations are ignored).
      

      I would say that this is the expected behaviour as the rotation will make the parent time move.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AlexMaly
        wrote on last edited by
        #3

        How about creating a QGraphicsItemGroup , add them both item and move the group, then you can rotate the child you need independently.
        Or just do what you do, but listen to the parent item QGraphicsItem::ItemRotationChange and adjust child item rotation there to compensate parent rotation?

        S 1 Reply Last reply
        2
        • S Offline
          S Offline
          sDmt
          wrote on last edited by
          #4

          Thank you SGaist for pointing out the flaw in my approach

          1 Reply Last reply
          0
          • A AlexMaly

            How about creating a QGraphicsItemGroup , add them both item and move the group, then you can rotate the child you need independently.
            Or just do what you do, but listen to the parent item QGraphicsItem::ItemRotationChange and adjust child item rotation there to compensate parent rotation?

            S Offline
            S Offline
            sDmt
            wrote on last edited by
            #5

            @AlexMaly Thanks for the valuable suggestion. I've managed to do it as you suggested

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved