Get the top most child of a Component


#1

Hi all,
I’m a Juce newbie trying to understand some basic principle.
I’m trying to retrieve the topmost component child of a given component using getComponentAt method. Seems not to work.

Component *foo = new Component();
Component *bar = new Component();
foo->setBounds(0,0,500,500);
foo->addChildComponent(bar);
bar->setBounds(10,10,400,400);

Component * target = foo->getComponentAt(100, 100);
if (target == NULL)
     DBG("COMPONENT NOT FOUND")

getComponentAt returns null.
What am I doing wrong? Did I misunderstand getComponentAt method ?


#2

getComponentAt only finds visible components!


#3

Sorry, I haven’t post all my code but it’s a bit more complex. Anyway the father component is visible (isVisible() returns true) and the child is correctly inserted with addAndMakeVisible.
I check also the bounds property of both components but still not works

Which could be the problem?


#4

Coordinate might not hit inside the child components bounds (did you set the child size ?)


#5

Yes I set the child size.
To be more specific I’m trying to do this:

I have a Component with inside several Slider .
This component is put inside another object that extends Component (MyComponent : public Component).

every child of MyComponent cannot intercept MouseClicks (setInterceptsMouseClicks(false, false):wink: , so the mouse events reach the MyComponent when I click on its visible childs (the slider). I see everything on the screen correctly.

Now my problem is that inside MyComponent (that implements MouseMove) I have to understand which of its child components would have intercept the mouse click. So I thought to use getComponentAt on its first child ( it’s the Component that has the sliders as children) but it seems not to work.


#6

No… getChildComponentAt is the method that’s used to find out which component a mouse event lands on, and it takes visibility and hit-testing into account. If you want to ignore all of that stuff and only test the component bounds, you’d need to write your own loop to do that.


#7

thanks for the info. I’ll implement my own method