Get Children of costum UUserWidget

Hellow,

is it possible to programmatically get all Widgets which are children of a custom UserWidget in c++?

like so:




void UMyUserWidget::MyCustomFunc()
{
    
    // example
    TArray<UWidget> Children = this->GetChildren();

}



Image i make a Widget Blueprint from UMyUserWidget and i add some Buttons in the Designertabs. And then GetChildren() would return those buttons (and other widgets of course).

thanks for your help

Hm, i only found that “UPanelWidget” and all underlying classes have Child Operation (maybe other classes too, but i haven’t found anything within
the UUserWidget class):

Too always have your widgets at hand, you should just add them to an Array when you add them at all. I did this in UMG and would do this in C++ too.
When you add a series of widgets, add them to an array. All other widgets, like pre placed buttons or so, already should have a variable. Otherwise you wouldn’t
be able to interact with them :smiley:

hey thanks for your fast reply.

I always add my desired widgets to an array, but i dislike to do it manually with each single widget.

so i am trying it like this now (not working yet):




/* in .h

TArray<UMyButtonWidget*> Buttons;

*/

void UMyUserWidget::Test()
{
UPanelSlot* RootPanel= Cast<UPanelSlot>(GetRootWidget()->Slot);

	if (OverlayWidget)
	{
		if (UVerticalBox* VertBox = Cast<UVerticalBox>(OverlayWidget->Content))
		{
			for (int32 i = 0; i < VertBox->GetChildrenCount(); i++)
			{
				if (UMyButtonWidget* btn = Cast<UMyButtonWidget>(VertBox->GetChildAt(i)))
				{
					Buttons.Add(btn);
					GEngine->AddOnScreenDebugMessage(3, 3, FColor::Red, btn->GetFName().ToString());
				}
			}
		}
	}
}


best regards

its me again :wink:

i was able to get the children this way:





void UMyUserWidget::GetMyChildren()
{

MyChildrenArray.Empty();

// Get the root panel widget (if you know its a panel, otherwise cast to the widget you have as root)
if (UOverlay* OverlayWidget = Cast<UOverlay>(GetRootWidget()))
{	
	// in my case the root overlay has a verticalbox as child, and since SOverlay only has ONE child i simply get the first one at index 0
	if (UVerticalBox* VertBox = Cast<UVerticalBox>(OverlayWidget->GetChildAt(0)))
	{	
	     // get the number of children inside the verticalbox to iterate through
	     int32 ChildrenCount = VertBox->GetChildrenCount();
                        
	     for (int32 i = 0; i < ChildrenCount; i++)
	     {
                   // if the current child is of widgettype i want to save add it to the array
		   if (UMyDesiredChildWidget* btn = Cast<UMyDesiredChildWidget>(VertBox->GetChildAt(i)))
		   {
			MyChildrenArray.Add(btn);
					
		   }
	     }			
	}
   }
}


Maybe you have a better idea to do it, but i felt like to share my solution as well :wink:

happy coding

You should use the WidgetTree’s ForEachWidget lambda iterator, that handles all the cases for dealing with named slots and such.

2 Likes

Hey,

thanks for this hint Nick, ill use it this way then :slight_smile:

kind regards