How to currectly get the size of user widget?

Hi, everyone:

I want to get the size of my widget in its own preconstruct event for preview layout, I have tried:
Self–>SlotAsCanvasSlot–>GetSize

but it only works when the anchor of this widget is top-left, I can only get the offset if the anchor is “top-fill”.
I have tried
Self–>GetCachedGeometry->GetLocalSize

but this only works at runtime.

Is anyone can tell me how to get the size of an widget in its own PreConstruct evenet? Thank a lot for your time.

Hi, everyone, I have found out what I need, it quite simple, just add a delay node before get the size from cached geometry:

In PreConstructEvent::

Delay(0);
RootCanvas–>GetCachedGeometry–>GetLocalSize

If you want to get the runtime size in widget tick, you can:

GetTickSpaceGeometry–>GetLocalSize

Hope that helps.

3 Likes

This works in blueprint too. Helps me preview how my widget will look in editor.

1 Like

C++ version to that only execute in designer/editor in case that helps anyone:

#if WITH_EDITOR
void URWidget::OnDesignerChanged(const FDesignerChangedEventArgs& EventArgs)
{
	Super::OnDesignerChanged(EventArgs);
	FTimerHandle TimerHandle;
	GetWorld()->GetTimerManager().SetTimer(TimerHandle, this, &ThisClass::MethodUsingCachedGeometry, 0.01, false);
}
#endif

This would do the same as what OP asked for but would only execute in the designer/editor, since your widget should be ticking and get the cached geometry without any issues otherwise.