# Cast UClass to UUserWidget

**URL:** <https://forums.unrealengine.com/t/cast-uclass-to-uuserwidget/399960>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [July 11, 2017, 2:08am UTC](https://forums.unrealengine.com/t/cast-uclass-to-uuserwidget/399960 "2017-07-11T02:08:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lanslot\_Chung](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Lanslot\_Chung](https://forums.unrealengine.com/u/Lanslot_Chung)\
**Post date:** [July 11, 2017, 2:08am UTC](https://forums.unrealengine.com/t/cast-uclass-to-uuserwidget/399960/1 "2017-07-11T02:08:18Z")

</div>

i wanna write a C++ function library, it contain a function called CreateAndAddWidget

.h file

```
UFUNCTION(BlueprintCallable, Category = "LBP/User Interface")
		UClass* CreateAndAddWidget(APlayerController* OwningPlayer, TSubclassOf<class UUserWidget> UserWidgetClass, int ZOrder = 1);

```

.cpp file

```
UClass* ULanslotBP::CreateAndAddWidget(APlayerController* OwningPlayer, TSubclassOf<class UUserWidget> UserWidgetClass, int ZOrder)
{
	UClass* wid = CreateWidget<UClass>(OwningPlayer, UserWidgetClass->GetClass());
	
	UUserWidget* a = Cast<UUserWidget>(wid);
}

```

i cannot cast uclass to uuserwidget, so i cannot call AddToViewport.  
Could you help me ?Or show me a new way?

---

<div class="post-metadata">

**Author:** ![AmphDev](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AmphDev](https://forums.unrealengine.com/u/AmphDev)\
**Post date:** [July 14, 2017, 1:10pm UTC](https://forums.unrealengine.com/t/cast-uclass-to-uuserwidget/399960/2 "2017-07-14T13:10:25Z")

</div>

Hey !  
Your solution is totally wrong.  
You try to use template, but your function isnt templatized, uclass return is useless, return UUserWidget instead of UClass… GetClass isnt needed…

Of course you can use template class function, but for this first read how templates works: [C++ Templates](https://www.tutorialspoint.com/cplusplus/cpp_templates.htm)

Anyway i highly recommend to do this in way:

.h

```
UUserWidget* CreateAndAddWidget(APlayerController* OwningPC, TSubclassOf<UUserWidget> WidgetClass;

```

.cpp

```
UUserWidget* YourClass::CreateAndAddWidget(APlayerController* OwnerPC, TSubclassOf<UUserWidget> WidgetClass)
{
   If(!WidgetClass)
       return nullptr;

   UUserWidget* wid = CreateWidget<UUserWidget>(OwnerPC, WidgetClass);

   If(wid)
        wid->AddToViewport(1);

  return wid;
}

```

In this case you dont need cast and every widget is based on uuserwidget, so safe to give back uuserwidget 🙂  
Cheera

---

<div class="post-metadata">

**Author:** ![Lanslot\_Chung](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Lanslot\_Chung](https://forums.unrealengine.com/u/Lanslot_Chung)\
**Post date:** [July 17, 2017, 9:31am UTC](https://forums.unrealengine.com/t/cast-uclass-to-uuserwidget/399960/3 "2017-07-17T09:31:43Z")

</div>

thanks for your answer;  
but i got some errors;

 ![202491-header.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/b/8/4b898d9ca972ca76d7622ce482009b6b309108d9.png)

 ![202492-cpp.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/7/6/57631d613a0285893402aa3c37ea32ff3eb9f183.png)

![202493-project_header.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/f/b/bfbb2e06f097a4eafb35e19984fb8b0d91c83e53.png)

 ![202494-build.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/f/3/2f3766ad6f44f85cdf3736ff75107ecbef29cc7a.png)

---

<div class="post-metadata">

**Author:** ![AmphDev](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AmphDev](https://forums.unrealengine.com/u/AmphDev)\
**Post date:** [July 17, 2017, 10:17am UTC](https://forums.unrealengine.com/t/cast-uclass-to-uuserwidget/399960/4 "2017-07-17T10:17:19Z")

</div>

Hey

Add UMG as public depedency 😉  
Include should Blueprint/UserWidget.h  
You need add Slate and SlateCore too for your build as public depedency

---

<div class="post-metadata">

**Author:** ![AmphDev](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AmphDev](https://forums.unrealengine.com/u/AmphDev)\
**Post date:** [July 17, 2017, 10:20am UTC](https://forums.unrealengine.com/t/cast-uclass-to-uuserwidget/399960/5 "2017-07-17T10:20:32Z")

</div>

This may help you.

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...
