Using a UMG widget in C++

I’m designing several widgets intended to go in a context menu. The Context menu itself is managed in C++ with Slate constructs and dynamic padding to get it to appear at the right spot.

When I try to use a widget designed in UMG, the widget does not render. There must be something I don’t understand.

The Context menu puts widget in a VerticalBox, named Container. This works for normal widgets :

void ContextMenu::AddWidget(TSharedRef<SWidget> Widget)
{
    SVerticalBox::FSlot& Slot = Container->AddSlot();
    Slot.AutoHeight();
    Slot[Widget];
}

To be able to also push UMG widgets in that method, I did this :

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=ContextActions)
TSubclassOf<UUserWidget> ButtonBlueprint;

...

UUserWidget* Widget = CreateWidget<UUserWidget>(GetWorld(), ButtonBlueprint);
ContextMenu->AddWidget(Widget->TakeWidget());

I’m not sure if TakeWidget() is the right way to grab the built Slate construct. I also centered the componennts of the widget in the left-top corner.

That looks like early 4.5/master - what you have looks right. But if your UMG root widget is a Canvas it has no innate desired size so if you give it a Top/Left layout in a slot instead of Fill/Fill, it wont take up any room, it will just be this 0,0 sized widget.

Cheers,
Nick

Depends on what you want - If you want it to fill the whole vertical box, then fill/1 is the right option, but since it’s a dropdown list, probably not. If there’s a specific size you KNOW the content will ALWAYS be, you can use SBox to hold the content.

Changing the root widget is probably what you want to do though, SizeBox for static sized content, Horizontal Box for flow content of variable size. Like if i were making a list of players with icons next to their name, my root widget choice would have been horizontal box with an image and textblock as child widgets.

Hey thanks Nick. I’m trying to figure out how to fix this properly. Would you say I gotta make a new UMG root widget type and use that instead, or rather set some properties on the slot? This being a vertical box slot, I can only set the height of it (as in FillHeight(1.f)).

What would you say is the best way to set a size?

Hey sorry for pestering you. The thing I don’t get is my UMG widget is basically the default Canvas at the root, then I put a Button in it, with LayoutData->Offsets set to fixed values, which are not zero, then a TextBlock in it with some text.

When the canvas wants to compute its desired size, won’t it ask its children, in this case the Button, which should itself return the LayoutData? Or even deeper, by asking the TextBlock what size it should be?

Just isn’t implemented - there’s a simple workaround so it’s not a high priority.

EDIT: Also not entirely sure how best to implement it, your case is trivial. But when you start using non-zero anchor points that rely on the size of the canvas to offset content or anchor points that are larger than 1 or smaller than 0, for cases where you want to record animation where things fly into the canvas - it gets pretty tricky.