Get parent UserWidget of Widget

Hello!

Is there any way to get UserWidget object that is parent for Widget (like a button, text block, etc.)

325305-ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅-2020-12-21-144640.png

325304-ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅-2020-12-21-144556.png

Get parent returns Widget element, but not UserWidget object

325306-ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅-2020-12-21-144906.png

8 Likes

I’m not sure I understand your question, but if you’re getting a widget as a return of GetParent it might mean your button is parented to a widget that in turn is part of your userwidget. In that case what you need to do is to call GetParent again on the output of your first GetParent; or anyway repeat this if you have nested widgets until you get to your userwidget

No, it doesn’t work :frowning:

I call GetParent() on CanvasPanel and it always returns null

325350-ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅-2020-12-21-213454.png

The method I use is to add a variable into the widget that is going to be the child of the class of the intended parent and expose it on spawn, then when you create the widget you can just add a reference to self to the input for that variable.

1 Like

Hey,
i was looking for this and i found the answer, you need to use the β€œouter object”

in my cas i was calling it in an child user widget

container userwidget  ( i want to get this )
    overlay
        canvas 
            child userwidget ( i'm calling from here )

24 Likes

unless you do fancy stuff with outer object allocation, this should return the first parent canvas

best regards
Paulo Egidio
Multiverse Designer

UCanvasPanel* GetParentCanvas(UWidget* widget)
{
	UCanvasPanel* Canvas = nullptr;
	do
	{
	startloop:
		auto candidate = widget->GetParent();
		if (candidate == nullptr)
		{
			// try outer object
			UObject* outter = widget;
			while (true)
			{
				outter = outter->GetOuter();
				if (outter == nullptr)
					return nullptr;
				auto w = Cast<UWidget>(outter);
				if (w != nullptr)
				{
					widget = w;
					goto startloop;
				}
			}
		}
		else
			widget = candidate;
		Canvas = Cast<UCanvasPanel>(widget);
	} while (Canvas == nullptr);
	return Canvas;
}

Excellent, this helped me a lot today: Parent->Outer->Outer->Cast works great. It seems that just Outer->Outer->Cast works as well, since the result from the first Outer is always the tree structure.

1 Like

I’m working on some UI now and just wanted to chime in since I stumbled upon this post.

Just a tip here. You don’t have to go through hierarchy like that. It becomes inefficient if you have a lot of elements.

Just use CommonUI Find Parent Widget of Type node if you are looking for an actual parent widget class of the sub widget and not the container element/s.

I have used 4 Get Outer Objects to reach my parent widget :smiley: