c++unreal-engine4unreal-umg

UE4 - Get children widgets from UUserWidget's pointer (UMG)


I got my non-UObject class holds an

UUserWidget* Instance;

So how do I get a children widget (Ex: UTextBlock) out of that

Briefly, I want something like this: Instance->GetChildrenWidgetByName("UTextBlock_Name")


Solution

  • In 4.26.2 you can do this if you know the child's name

    Instance->WidgetTree->FindWidget(WidgetFName);
    

    or this if you just want to find it by type

    TArray<UWidget*> Children;
    Instance->WidgetTree->GetAllWidgets(Children);
    for(auto Child : Children)
    {
        if(UTextBlock* Block = Cast<UTextBlock>(Child))
        {
           //return Block
           //or add to some TArray if you want to find multiple
        }
    }
    

    for more information take a look into documentation

    on a side note a very handy way of getting pointers to children of a widget is to use meta = (BindWidget) in UPROPERTY - more on that here