Results 1 to 3 of 3

Thread: Child GraphicsItems Not Getting itemChange()

  1. #1
    Join Date
    Oct 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question Child GraphicsItems Not Getting itemChange()

    I have two QGraphicsRectItem objects with one being the child of the other. So I have a parent and a child.

    I'm trying to track the movement of the child whenever the parent is moved.

    These two flags are set for both object.
    setFlag(ItemIsMovable);
    setFlag(ItemSendsGeometryChanges);

    When the parent item is moved itemChanged() is called with ItemPositionHasChanged,
    but the child's is never called. I tried using ItemParentHasChanged, but that isn't being called.

    Anyone have any thoughts? Here's the function:

    Qt Code:
    1. QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
    2. {
    3. switch (change) {
    4. case ItemPositionHasChanged:
    5. qDebug() << "ItemPositionHasChanged";
    6. break;
    7.  
    8. case ItemParentHasChanged:
    9. qDebug() << "ItemParentHasChanged;
    10. break;
    11. default:
    12. break;
    13. };
    14.  
    15. return QGraphicsItem::itemChange(change, value);
    16. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 20th October 2010 at 01:17.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Child GraphicsItems Not Getting itemChange()

    The child's method will only be called when it moves relative to the parent. ItemParentHasChanged would be called if you set a different parent for an item. The only thing that possibly changes in the situation you describe is the scene position but there is no itemChange() version of that (and good, it would be terribly slow).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Child GraphicsItems Not Getting itemChange()

    Quote Originally Posted by wysota View Post
    The child's method will only be called when it moves relative to the parent. ItemParentHasChanged would be called if you set a different parent for an item. The only thing that possibly changes in the situation you describe is the scene position but there is no itemChange() version of that (and good, it would be terribly slow).
    Yeah, I see that itemChange() is only called when a particular item is moved and not when it's parent is moved as well.

    I totally misread ItemParentHasChanged, you are right, it's about an item's parent changing and I misread it as the parent position changing. duh! heh

    While typing this response I figured out how to do what I was trying to do.

    Basically I was trying to do something like the elastic nodes example with arrows connecting nodes, but instead have arrows connecting child nodes. So what I needed was a way to update the arrows when the parent nodes moved.

    I solved this by doing the following:

    Qt Code:
    1. QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
    2. {
    3. QList<QGraphicsItem*> children;
    4. switch (change) {
    5. case ItemPositionHasChanged:
    6. children = this->childItems();
    7. foreach(QGraphicsItem* child, children) {
    8. static_cast<Node*>(child)->updateEdges();
    9. }
    10. this->updateEdges();
    11. break;
    12. default:
    13. break;
    14. };
    15.  
    16. return QGraphicsItem::itemChange(change, value);
    17. }
    18.  
    19. void Node::updateEdges() {
    20. foreach(Arrow* arrow, m_arrows)
    21. arrow->adjust();
    22. }
    To copy to clipboard, switch view to plain text mode 

    So what happens is when the parent's itemChange() is called, it gets a list of it's children and tells them to update their arrows.

    Thanks for the reply it got me thinking and back on track to solving this.

Similar Threads

  1. move GraphicsItems within a GraphicsScene
    By rambo83 in forum Newbie
    Replies: 7
    Last Post: 4th November 2009, 14:40
  2. itemChange method
    By zgulser in forum Qt Programming
    Replies: 4
    Last Post: 17th August 2009, 09:19
  3. Moving Items with itemChange?
    By konvex in forum Qt Programming
    Replies: 5
    Last Post: 21st November 2008, 14:36
  4. GraphicsItems and layouts
    By godmodder in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2008, 08:43
  5. itemChange() glibc invalid pointer
    By dreamer in forum Qt Programming
    Replies: 2
    Last Post: 3rd May 2008, 09:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.