Mouse down on child needs to tell parent to kill the child

i want to right click on children components to kill them.   

right now, i can intercept the right click within the child, no problem.  But i'm struggling with how to send that event back to the parent, so it can kill the child, and clean up afterwards.  Just passing a pointer or reference to the parent is not working, as it gives a circular dependency. 


(i assume i'm on some sort of watchlist now after typing all that out?)  






A component automatically remove itself from the parent child list when it is destroyed. Have a look at ~Component() destructor. So you can perform a delete this from within mouseDown(), provided that you don't manipulates the object then.

you can try making your child components change broadcasters and your parent a change listener. 

register your parent as a changelistener for the child and when the right click delete is called send a broadcast to the parent to delete it.


also, maybe your circular dependency is from an include in the header file? a couple times i've fixed my issue with that by move the include statement to the cpp file where i actually need to access the data 

depending on what your child is, you could just do hit-testing within the parent to see if the mouseEvent landed within the bounds of a child.   Why does the child need to capture mouse events in the first place? 



will remember that in future.  seems like it shoudl work ok. 

yes, that's what i have ended up doing. 



i was super scared of calling delete from within an object.  kinda felt like it would make stuff blow up. 

I did try it though, and apparently it does work.  In this case i went for a different workaround, but nice to know it is possible if i ever really need to do that. 




...or you could register your parent as a MouseListener for the childs, and then discern the Component that has been clicked by looking at the originComponent member of the MouseEvent that is passed to your Parent::mouseUp() ?

1 Like