This brief article addresses the question of how to cast one Slate widget to another in Unreal Engine. Imagine you have an SWidget widget wrapped in a shared pointer like this:

TSharedPtr<SWidget> MyWidgetPtr;

If you want to cast it, for example, to an SSpacer, your code might look like this:

TSharedPtr<SSpacer> MySpacerPtr = StaticCastSharedPtr<SSpacer>(MyWidgetPtr);

It’s important to keep in mind that you should verify whether the shared pointer is valid before using it.

The same approach works for shared references:

TSharedRef<SWidget> MyWidgetRef;
TSharedRef<SSpacer> MySpacerRef = StaticCastSharedRef<SSpacer>(MyWidgetRef);

Alternatively, you can perform the casts manually, directly using static_cast or dynamic_cast with the widget. However, be aware that this approach may not be as reliable as the one provided above, so use it with caution.