# Set C++ widget class from code

**URL:** <https://forums.unrealengine.com/t/set-c-widget-class-from-code/367152>\
**Category:** UI\
**Tags:** unreal-engine, Widget, UE4, Blueprint, CPP, question, UMG\
**Created:** [August 14, 2016, 11:00pm UTC](https://forums.unrealengine.com/t/set-c-widget-class-from-code/367152 "2016-08-14T23:00:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_30deeed0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_30deeed0](https://forums.unrealengine.com/u/anonymous_user_30deeed0)\
**Post date:** [August 14, 2016, 11:00pm UTC](https://forums.unrealengine.com/t/set-c-widget-class-from-code/367152/1 "2016-08-14T23:00:17Z")

</div>

I have a widget blueprint that i want to add to a pawn as a UI element. I can do this in blueprints by adding a 3d widget component and setting its class to my widget blueprint. I want to do this in C++, however. I have the widget component up but I am trying to set its class to the widget blueprint reference and I seem to be having issues with that. I need this to be a 3d widget since this is for VR.

Here is what I have now.

.h

```
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Widgets")
	class UWidgetComponent* CoolWidget;

```

.cpp constructor

```
static ConstructorHelpers::FObjectFinder<TSubclassOf<UUserWidget>> Cool_use(TEXT("WidgetBlueprint'/Game/Blueprints/Panel_Cool.Panel_Cool'"));
	if (Cool_use.Succeeded()) {
		CoolWidget->SetWidgetClass(*Cool_use.Object);
	}

```

This crashes the game engine, when I take away the pointer from Cool\_use.Object the error I get is that it cannot convert TSubClassOf \* to TSubClassOf. When I keep it, the error is that to see reference to function template instantiation.

The question I have is very straightforward. How do I properly set the Widget Class to a widget blueprint reference?

Thanks!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_86746194](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_86746194](https://forums.unrealengine.com/u/anonymous_user_86746194)\
**Post date:** [August 15, 2016, 2:26am UTC](https://forums.unrealengine.com/t/set-c-widget-class-from-code/367152/2 "2016-08-15T02:26:18Z")

</div>

static ConstructorHelpers::FClassFinder Cool\_use(TEXT(“/Game/Blueprints/Panel\_Cool”));  
if (Cool\_use.Succeeded())  
{  
CoolWidget-\>SetWidgetClass(Cool\_use.Class);  
}

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_30deeed0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_30deeed0](https://forums.unrealengine.com/u/anonymous_user_30deeed0)\
**Post date:** [August 15, 2016, 2:13pm UTC](https://forums.unrealengine.com/t/set-c-widget-class-from-code/367152/3 "2016-08-15T14:13:44Z")

</div>

That worked, however I needed to restart the editor after compiling for it to actually show up. Thank you!!

---

<div class="post-metadata">

**Author:** ![ShadowSprite123](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ShadowSprite123](https://forums.unrealengine.com/u/ShadowSprite123)\
**Post date:** [June 6, 2023, 8:24am UTC](https://forums.unrealengine.com/t/set-c-widget-class-from-code/367152/4 "2023-06-06T08:24:17Z")

</div>

Gets an error. Any fix?
