I currently am creating a custom Component (x). The Component has a button that when pressed, adds a new Component (y) on the screen that is owned and managed by the main Component (x). You can hit this button many times and add more of these child Components, which are stored in a std::vector in the main Component (x). The child Components are laid out in a Flexbox that is regenerated every time a new child is added. Each of the child Components have a remove button. The goal is to be able to remove a child component by clicking its remove button, but I need to be able to tell the parent structure (x) that this deletion has occurred.
Currently, the vector in the main Component uses SafePointers that point to the children and the children call delete this
whenever the remove button on them is pressed. This will then cause the associated SafePointer in the vector to become null.
The issue is that I don’t know how to tell the main Component (x) about this deletion, so it doesn’t know to recall the layout function, which leaves the UI looking as if there is a missing widget. I can’t use findParentComponentOfClass<>()
inside of the remove button code to call the relayout function in the parent structure, because the remove code deletes the object itself, making it no longer valid, thus not being able to make execute any code after.
I found componentBeingDeleted (Component &component)
, so I thought I’d try adding a listener to the components being deleted, so I could call the relayout function, but what I’ve found is that it doesn’t execute after the deletion every time, it sometimes executes before the deletion finishes, so I can’t reliably use that.
I’ve hacked a solution that sort of works, but isn’t ideal. The solution I’m currently using is to not delete the child when the remove button is clicked, but to set a Boolean variable that will be used in the parent structure. Then, after, in the remove code, call the relayout of the parent structure using findParentComponentOfClass<>()
. This leaves a bunch of null pointing pointers in the vector, which are then cleaned out whenever the button to add new children is clicked. This isn’t ideal. I would really like for the parent object to know whenever a child is deleted, so I can remove it from the vector and relayout the remaining children.
Does anyone have any suggestions for this type of thing?