UserWidget added as Child: always wrong size


I have programmed a minimap-widget which is derived from UUserWidget.

All actors in the world should be represented on the minimap by an own derived actor-UserWidget, which changes its behavior itself.

For example, if the related actor is dead, the actor-widget itself should become invisible.

So I have a minimap-widget with child-actor-widgets.

However, if I add the actor-widgets to the minimap-widget with UPanelWidget::AddChild, the widgets always have a wrong size.

It should be 100100, but it looks like 20050.

Is this a bug ?

In the UMG-designer I can add my custom actor widget to the minimap widget, and it looks correct.

Can you help ? Any clues ?


I am not sure this can be answered without knowing some more about your widget. But you should bear in mind that UMG-designer does some layout stuff automatically when placing items, that you might have to do explictly in code/bluepints (i.e taking the slot after you add a child, and setting properties).

Did you solve this? I’m seeing the same issue. I suspect I am missing something important about how layouts are calculated, and that I need a special canvas as root along with proper anchors etc, but I just can’t figure it out…
My “child” has the size 64x64 in its own Widget-file (an image) (even tried with a SizeBox as root, with overridden 64x64 size), but when I add it to the parent using ->AddChild() it shows up as 100x30 or something. I’ve tried all combinations of anchors and SizeToContent etc I can think of, but it’s just plain wrong.

I’m not sure if it’s possible to add a child widget to primitives (such as UBorder, UButton and etc) with it’s own size, but for canvas panel you can use UCanvasPanelSlot* UCanvasPanel::AddChildToCanvas(UWidget* Content) and after that use void UCanvasPanelSlot::SetSize(FVector2D InSize).
For example:

	auto NewSpotRaw = WidgetTree->ConstructWidget<UUserWidgetChild>(UUserWidgetChild::StaticClass()); // create child of UUserWidget
	UCanvasPanelSlot* NewSlot = CanvasPanel->AddChildToCanvas(NewSpotRaw); // add child to canvas panel and save new slot to variable
	NewSlot->SetAlignment({ 0.5f, 0.5f }); // set some settings (pivot of new slot)
	NewSlot->SetPosition(FVector2d(250.0f)); // set  another some settings (position on screen)
	NewSlot->SetSize({ 30.0f, 30.0f }); // set size 30x30
1 Like