# Casting slate widgets

**URL:** <https://forums.unrealengine.com/t/casting-slate-widgets/315587>\
**Category:** UI\
**Tags:** casting, unreal-engine, slate, CPP, question\
**Created:** [April 19, 2015, 9:29am UTC](https://forums.unrealengine.com/t/casting-slate-widgets/315587 "2015-04-19T09:29:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_cf61193c1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_cf61193c1](https://forums.unrealengine.com/u/anonymous_user_cf61193c1)\
**Post date:** [April 19, 2015, 9:29am UTC](https://forums.unrealengine.com/t/casting-slate-widgets/315587/1 "2015-04-19T09:29:11Z")

</div>

Hi!

This is probably a simple question:  
How do i cast from SWidget to STextBlock?

I tried for some hours now. So far nothing works.  
Here are some things i have already tried:

```
STextBlock textBlock = (STextBlock)this->ContentSlot->GetWidget().Get();
//Error C2440 : 'type cast' : cannot convert from 'SWidget' to 'STextBlock'

STextBlock textBlock = dynamic_cast<STextBlock>(this->ContentSlot->GetWidget().Get());
//Error C2680: 'STextBlock' : invalid target type for dynamic_cast

```

I feel its something simple and obvious, but i could use someone to point it out to me 🙂

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_c788a779](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_c788a779](https://forums.unrealengine.com/u/anonymous_user_c788a779)\
**Post date:** [April 19, 2015, 11:08am UTC](https://forums.unrealengine.com/t/casting-slate-widgets/315587/2 "2015-04-19T11:08:31Z")

</div>

You’re trying to cast to a value type, which means you’re telling the compiler to try to construct a _new_ object from the result of Get(). Your first line is equivalent to using static\_cast, but still all that means is constructing an STextBlock from an SWidget, instead of casting it.

What you want to do is cast the _reference_, like so:

`static_cast< STextBlock& >(this->ContentSlot->GetWidget().Get())`

This way, you’re not copying the object, you’re casting a reference to it. The choice between static\_cast and dynamic\_cast is yours, although dynamic\_cast can only be used with a pointer, since references are not nullable:

`dynamic_cast< STextBlock* >(&this->ContentSlot->GetWidget().Get())`

More excellent reading on the subject [here](http://www.aristeia.com/EC3E/3E_item27.pdf).

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_cf61193c1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_cf61193c1](https://forums.unrealengine.com/u/anonymous_user_cf61193c1)\
**Post date:** [April 20, 2015, 1:17pm UTC](https://forums.unrealengine.com/t/casting-slate-widgets/315587/3 "2015-04-20T13:17:28Z")

</div>

Using dynamic\_cast gives me this error: C4541: ‘dynamic\_cast’ used on polymorphic type ‘SWidget’ with /GR-; unpredictable behavior may result

I will have to read on that.

Your static\_cast solution works fine. So, thanks for your help 🙂  
Coming from C# (Not Unity, but business application dev) all that casting, pointer and reference stuff is mysterious.  
The good thing with UE4 is that i finally have a reason to learn c++

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_c788a779](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_c788a779](https://forums.unrealengine.com/u/anonymous_user_c788a779)\
**Post date:** [April 21, 2015, 9:17am UTC](https://forums.unrealengine.com/t/casting-slate-widgets/315587/4 "2015-04-21T09:17:48Z")

</div>

Ah yes, in Unreal you would just use Cast\<\> instead of dynamic\_cast\<\>, as by default Unreal disables an option on the compiler that prevents usage of the native dynamic\_cast. However, Cast\<\> only works on UObjects, so with your SWidgets you’ll be limited to static\_cast, which will yield undefined behavior if the thing you’re casting is not actually of the type you’re looking for.

---

<div class="post-metadata">

**Author:** ![ellocator](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ellocator](https://forums.unrealengine.com/u/ellocator)\
**Post date:** [May 14, 2018, 4:24pm UTC](https://forums.unrealengine.com/t/casting-slate-widgets/315587/5 "2018-05-14T16:24:37Z")

</div>

To protect against the undefined behavior of static\_cast you can do a check like this:

```
TSharedPtr<SWidget> widget;
if (widget->GetType() == "SObjectWidget")
{
    SObjectWidget* objectWidget = static_cast<SObjectWidget*>(widget.Get());
}
```

---

<div class="post-metadata">

**Author:** ![Repin\_Design](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/repin_design/32/598391_2.png) [@Repin\_Design](https://forums.unrealengine.com/u/Repin_Design)\
**Post date:** [September 10, 2020, 9:22pm UTC](https://forums.unrealengine.com/t/casting-slate-widgets/315587/6 "2020-09-10T21:22:17Z")

</div>

Thank u guys, this post solved all of my problems.

---

<div class="post-metadata">

**Author:** ![Emaer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/emaer/32/570877_2.png) [@Emaer](https://forums.unrealengine.com/u/Emaer)\
**Post date:** [September 13, 2020, 9:30pm UTC](https://forums.unrealengine.com/t/casting-slate-widgets/315587/7 "2020-09-13T21:30:15Z")

</div>

You can use `StaticCastSharedPtr`

> **[StaticCastSharedPtr](https://docs.unrealengine.com/4.27/en-US/API/Runtime/Core/Templates/StaticCastSharedPtr/)**
>
> Casts a shared pointer of one type to another type. (static\_cast) Useful for down-casting.

---

<div class="post-metadata">

**Author:** ![muchcharles](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@muchcharles](https://forums.unrealengine.com/u/muchcharles)\
**Post date:** [September 10, 2024, 11:41pm UTC](https://forums.unrealengine.com/t/casting-slate-widgets/315587/8 "2024-09-10T23:41:53Z")

</div>

I think likw static\_cast, StaticCastSharedPtr does not do anything to check the type so still needs `if (widget->GetType() == "typename")` before if you don’t know for sure it is safe to downcast.

Is there any utility function that casts it and checks the type, setting the TSharedPtr to null/Invalid on fail? I think GetType gives an FName, so it is maybe not possible without a macro, or C++ reflection whenever that gets added?
