parentHierarchyChanged() - is it relevant name of method?

Hello,
as name of method suggests it is called always the parent hierarchy is changed and no matter what child was added or removed.

So let’s say I have:
myChildComponent01
and
myChildComponent02
and
myParentComponent.

And now when I call:
myParentComponent.addAndMakeVisible(myChildComponent01);
then inside of it is called:
myChildComponent01.parentHierarchyChanged();

That’s great. And now myParentComponent is parent of myChildComponent01. So next I call:
myParentComponent.addAndMakeVisible(myChildComponent02);

As you can see I added another child, but I expect myParentComponent is also still parent of my first child. So I expect myChildComponent01.parentHierarchyChanged(); should be called again when another child is added. But it doesn’t happen.

Actually I prefer that behaviour, but I need to be sure parentHierarchyChanged() is called only once for each child, but the name of that method suggests it is called every time I add any childe to parent. So I am not sure what can I expect?

For any help great thanks in advance.
Best regards

Language is a slippery thing. To me, this is very clear. Since the callback comes to a child component, it reflects changes to the parentage of that child. Creating a sibling does not change parentage of the first child, thus no callback.

And, while a function name should be useful, it’s not really documentation. The actual documentation for this function is clear on how it works. Your child component will get a callback when it’s parent hierarchy changes.

1 Like

I wouldn’t expect this to happen - child components shouldn’t necessarily know about each other, only the parent should know about its children.

From the perspective of myChildComponent01 nothing has changed in the parent hierarchy - it’s parent hasn’t changed, nor any of its ancestors.

As @cpr2323 says, language can be tricky. To me, parentHierarchyChanged() implies that it will only ever be called when the parent, or an ancestor, changes for the given component. I wouldn’t except that to be called when another child is added to the parent.

OK, great thanks for your answer. So it looks like I can be sure that:
myChildComponent.parentHierarchyChanged();
will be called only when myChildComponent is added/removed from it’s parent, but it is not called when other children are added/removed to the same parent.

That knowledge is enough for me. Thanks.

1 Like