Get size of UMG widget

Hey, I want to get the size of a widget inside a child class of UHorizontalBox in C++. Is that possible? GetDesiredSize() always returns 0 and I hadn’t have any luck with with anything else I tried so far.

I have this problem too in UE 4.12.

I also have this problem in 4.11

I have found out, that there are two different approaches for this question.

1.: If you want to get the size during initialization of an actor, for example in BeginPlay, when the Geometry of the widget has not already been calculated, you can use the following code:

 UPanelSlot* examplePanelSlot = ExamplePanelWidget->Slot;
 UCanvasPanelSlot* exampleCanvasPanelSlot = Cast<UCanvasPanelSlot>(examplePanelSlot);
 FVector2D examplePanelSize;
 if (NULL != exampleCanvasPanelSlot) {
    	examplePanelSize = exampleCanvasPanelSlot->GetSize();
 }

But this only works, if the UUserWidget* ExamplePanelWidget actually has an UCanvasPanelSlot, for example if you’re using an Overlay or HorizontalBox.

You can see the kind of Slot in the UMG Blueprint, if you select the widget and go to the Details-Panel, for example:

134416-ucanvaspanelslotexample.png

2.:You can get the size during runtime from the Geometry of the widget:

  FGeometry cachedGeometry = ExamplePanelWidget->GetCachedGeometry();
  FVector2D currentSize = cachedGeometry.GetLocalSize();

But if you try this before the Geometry of the Widget was calculated the first time – for example during BeginPlay – you will get a zero vector.

I’m over this right now, as I want to fill a vertical box with as many text widgets as fit in without scrolling. But the size of the text can vary as well as the rendersize of the widget.

I got myself to the part with the cachedGeometry and that is 0 at time of building the widget (in RebuildWidget). But at what point is the geometry calculated? Can I force the calculation? The widget is placed in a WidgetComponent.

Thanks, you are right