Originally Posted by
wysota
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:
{
QList<QGraphicsItem*> children;
switch (change) {
case ItemPositionHasChanged:
children = this->childItems();
static_cast<Node*>(child)->updateEdges();
}
this->updateEdges();
break;
default:
break;
};
}
void Node::updateEdges() {
foreach(Arrow* arrow, m_arrows)
arrow->adjust();
}
QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
{
QList<QGraphicsItem*> children;
switch (change) {
case ItemPositionHasChanged:
children = this->childItems();
foreach(QGraphicsItem* child, children) {
static_cast<Node*>(child)->updateEdges();
}
this->updateEdges();
break;
default:
break;
};
return QGraphicsItem::itemChange(change, value);
}
void Node::updateEdges() {
foreach(Arrow* arrow, m_arrows)
arrow->adjust();
}
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.
Bookmarks