Set width of parent component in relation to their child component?

Hi all,

let’s say I have a main component with eight child components. All child components should lay out side by side and have the same height (= height of the parent component). The width of the individual child components may vary.

My question: how can I get the parent component height stay the same but adapt the width to be as large as all child components together? In other words, how can I set the size of the parent component in relation to its child components sizes/proportions?


I’d say: register your parent component as a ComponentListener for the child components.

When you receive a componentMovedOrResized callback because one of those wasResized, then reposition them side-by-side as you desire (watch out because this causes componentMovedOrResized to be called as well, with wasMoved = true and wasResized = false) and finally set the width of the parent component to match their total width.

Thanks yfede,

that doesn’t work, unfortunately, as the child components get moved and resized every time the height of the parent component changes and then I get an infinite loop of resizing between child and parent. And the first child component is always at position 0,0 so wasMoved is never true.
However, what is working is calling getParentComponent()->resized() for at least getting everything redrawn correctly. But I read that resized() should never be called directly. Why is that?
Another approach I’m actually trying is Component::postCommandMessage(int commandId) and Component::handleCommandMessage(int commandId).
I think this should do the trick for reordering the child components and resizing the parent component in one function. The first tests look promising but I didn’t have enough time to do a full test.

Any suggestion on that approach?


You could always add a function to your parent Component that the child Components can call that tells it what size to make which component (using its name to identify it or something). Then you could simply compute the required size of the parent, and set it there. Then, in resized(), you’d move and resize the children as needed (using the size passed from the child that you wanted to change, and keeping the sizes of the others). Then you’d never have any circular logic going on.