Download

How to get parent UUserWidget?

Hey everyone,

I have a question related to UMG. I have a UUserWidget and I am trying to get the next UUserWidget that contains it. My initial intention was to use UWidget::GetParent() function, however I realized that for every widget whose parent is UUserWidget the UWidget::GetParent() returns null.

The reason for that is the fact that UWidget::GetParent() expects to have a UPanelWidget slot as its parent and UUserWidget is not a UPanelWidget and it doesn’t have a slot. Instead it seems to be keeping all its content inside a UWidgetTree.

Is there a workaround for that?

Here’s the code I was trying to use which doesn’t work. Posting it here just to help to illustrate the idea:



const FBuxWidgetContextData* UBuxUserWidget::GetWidgetContextData() const
{
    FBuxWidgetContextData* Result = nullptr;

    UWidget* Parent = GetParent();
    while (Parent)
    {
        UBuxContextWidget* ContextWidget = Cast<UBuxContextWidget>(Parent);
        if (ContextWidget)
        {
            Result = &ContextWidget->Context;
            break;
        }

        Parent = Parent->GetParent();
    }

    if (!Result)
    {
        UE_LOG(LogBuxWidgets, Warning, TEXT("Missing context for %s"), *GetPathName());
    }

    return Result;
}


[USER=“2522”]Nick Darnell[/USER] ?

Thanks,

Robert.

When you reach a null parent, look at the Outer, try and cast it to a widget tree, then the outer of the widget tree is the userwidget that owns it. You can then repeat that process, starting with getting the parent of that new UserWidget.

[USER=“2522”]Nick Darnell[/USER] Thank you!

Check out Rama’s plugin for GetParentOfClass().
Shared that a long while ago along with some other misc UMG related functions.