C++ Adding Widget to WidgetTree doesn't update Hierarchy view in editor

I’m adding a UIMage to the WidgetTree RootWidget for a custom cursor class using UUserWidget. While this works and the design view show the image the Hierarchy view that lists the widgets continues to show the Canvas panel that is automatically added.

In the UMGEditor the hierarcy view is managed by a WidgetTreeView but I can’t tell how to tell it to update it’s view of the items in the UUserWidget.

I override the RebuildWidget likes this:

      if(MyImage == nullptr)
      {
        MyImage = WidgetTree->ConstructWidget<UImage>(UImage::StaticClass(), TEXT("CursorImage"));
        WidgetTree->RootWidget = MyImage;
        if(IsDesignTime()) {
          WidgetTree->SetFlags(RF_Transactional);
          WidgetTree->Modify();
        }
        FName DefaultText = TEXT("Default");
        SetCursor(DefaultText);
      }
    
      return UUserWidget::RebuildWidget();

If a default texture has been set it even display in the design view. Just can’t get that menu on the side to update.

Did you find a solution for this? I am having the same problem

I have same problem, too :frowning:

That’s my problem too. Please anybody, share at least your thoughts about it.

I have same problem, too :frowning:

I have same problem, any one can help!!!!!!!!!!!1

Maybe because you’re rebuilding the widget after you’re setting everything?

You can editor widget in blueprint.And use WidgetTree->FindWidget() anywhere you want in cpp.

have anyone solve this problem?

I have same problem, too :frowning:

geez still no solution?

Same problem :frowning:

Hey,

Sorry to revive the thread after so long, but I stumbled upon it when I was looking for an answer for the same issue so it might be worth finally answering it after I sorted it out for myself.

It was easier to understand after reading this article from Ben’s UI:

Basically I believe in order for the changes to be propagated correctly you will need to do several things:

  1. Get a reference to the Widget Blueprint as this is what you will need to be modifying:
const UWidgetBlueprintGeneratedClass* WidgetBlueprintGeneratedClass = Cast<UWidgetBlueprintGeneratedClass>(BaseWidget->GetClass());
const UPackage* Package = WidgetBlueprintGeneratedClass->GetPackage();
UWidgetBlueprint* MainAsset = Cast<UWidgetBlueprint>(Package->FindAssetInPackage());
  1. From that blueprint that you just got, find a reference to the widget you are trying to modify, in my case it was a CanvasPanel:
UCanvasPanel* FoundCanvasPanel = Cast<UCanvasPanel>(MainAsset->WidgetTree->FindWidget(BaseWidget->GetCanvasPanelName()));
  1. Construct the widget and assign it to the canvas or do whatever else you need to do with it:
FString WidgetName = FString::Printf(TEXT("Widget_%d"),i);
UUserWidget* PreviewUserWidget = MainAsset->WidgetTree->ConstructWidget<UUserWidget>(BaseWidget->WidgetToCreateClass, FName(WidgetName));

if(IsValid(PreviewUserWidget))
{
    UNodeWidget* NewWidget = Cast<UNodeWidget>(PreviewUserWidget);
    if(IsValid(NewWidget))
    {
        UPanelSlot* NewAddedSlot = FoundCanvasPanel->AddChild(NewWidget);
    }
  1. To propagate all of the changes so they do not get lost you will need to mark all of the assets that have been modified as “dirty” using Modify() and mark the blueprint as structurally modified:
FoundCanvasPanel->Modify();
MainAsset->Modify();

FBlueprintEditorUtils::MarkBlueprintAsStructurallyModified(MainAsset);

This code worked for me and it added my widgets to the Hierarchy and the screen.

I know this is a bit late :sweat_smile:, but I hope whoever finds this will have to struggle less than I did!