How to get useful parent of Widget


05-05-2013 01:24:36

So I have a window named "Window" and on it is a button. If I have a pointer to the button, how do I reliably get a pointer to the window it's on?

button->getParent()->getName(); // Returns "client"
button->getParent()->getParent()->getName(); // Returns empty
button->getParent()->getParent()->getParent()->getName(); // Returns "Window"

So I understand that widgets are composed of multiple widgets. But since one can take a button widget and attach it to a window widget, it seems reasonable that one could then ask that button what widget it's attached to. Calling getParent() three times works for a button on a window, but is that reliable for all widgets on all other widgets?

05-05-2013 19:39:26



05-05-2013 21:17:28

Thank you, but what if you don't know the name? The situation above where you have a window named "Window" and a button is just a simple example. I'm trying to imagine a situation where widgets are created and manipulated dynamically, so you don't know ahead of time what widgets will exist while the program is running or what their names will be.

The only solution I can think of is to keep a mapping of child to parent widgets using userData. At the moment you attach a widget to a new parent widget, you have pointers to both, so set the child widget's userData to the pointer of the parent widget.

But is that really the best way? Is there really no reliable method to simply ask a widget for its parent?