# How to initialize custom widgets

**URL:** <https://forums.unrealengine.com/t/how-to-initialize-custom-widgets/35716>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [July 17, 2015, 3:53pm UTC](https://forums.unrealengine.com/t/how-to-initialize-custom-widgets/35716 "2015-07-17T15:53:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_094df259](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_094df259](https://forums.unrealengine.com/u/anonymous_user_094df259)\
**Post date:** [July 17, 2015, 3:53pm UTC](https://forums.unrealengine.com/t/how-to-initialize-custom-widgets/35716/1 "2015-07-17T15:53:38Z")

</div>

Hi,  
I’d like to create a custom widget that can be initialized based on some game state.  
So I wrote the following:

weapon\_vbox.h

```auto

#pragma once

#include "Blueprint/UserWidget.h"
#include "Components/VerticalBox.h"

#include "weapon_vbox.generated.h"

UCLASS(BlueprintType)
class MYGAME_API Uweapon_vbox : public UVerticalBox
{
	GENERATED_BODY()

protected:
	void OnWidgetRebuilt();
};

```

weapon\_vbox.cpp

```auto

#include "MyGame.h"

#include "weapon_vbox.h"

#include "Components/TextBlock.h"

void Uweapon_vbox::OnWidgetRebuilt()
{
	Super::OnWidgetRebuilt();
	UTextBlock *tester_text = CreateWidget<UTextBlock>(GetWorld(), UTextBlock::StaticClass());
	
	tester_text->SetText(FText::FromString(FString(TEXT("rock on"))));
	this->AddChild(tester_text);
}

```

Unfortunately as soon as I try to use this widget in a WidgetBlueprint the editor crashes.

I guess I didn’t put the initialization in the right place? And/or didn’t add the child node correctly?

So I’d like to initialize my widget with data that is not available at design time. How should I do that?

Thanks.

UE 4.7.1

---

<div class="post-metadata">

**Author:** ![kamrann](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kamrann](https://forums.unrealengine.com/u/kamrann)\
**Post date:** [July 18, 2015, 8:33am UTC](https://forums.unrealengine.com/t/how-to-initialize-custom-widgets/35716/2 "2015-07-18T08:33:59Z")

</div>

_CreateWidget_ can only be called for _UUserWidget_-derived classes, not _UWidget_-derived components.  
I’m afraid I don’t know what the process is for constructing _UWidget_s dynamically in code - I’ve never tried to do it nor have I seen anything related to it.

---

<div class="post-metadata">

**Author:** ![Bn\_Green](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bn\_Green](https://forums.unrealengine.com/u/Bn_Green)\
**Post date:** [July 18, 2015, 5:09pm UTC](https://forums.unrealengine.com/t/how-to-initialize-custom-widgets/35716/3 "2015-07-18T17:09:37Z")

</div>

Try this wiki you may find something to use

> **[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...

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_094df259](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_094df259](https://forums.unrealengine.com/u/anonymous_user_094df259)\
**Post date:** [July 19, 2015, 1:12pm UTC](https://forums.unrealengine.com/t/how-to-initialize-custom-widgets/35716/4 "2015-07-19T13:12:27Z")

</div>

Thanks for your answers!

So if I understand correctly a “UUserWidget-derived class” is what I get when I do “Add New -\> User Interface -\> Widget Blueprint”?  
And adding elements to _UPanelWidget_s is only possible at design time?

Thanks.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_094df259](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_094df259](https://forums.unrealengine.com/u/anonymous_user_094df259)\
**Post date:** [July 19, 2015, 5:11pm UTC](https://forums.unrealengine.com/t/how-to-initialize-custom-widgets/35716/5 "2015-07-19T17:11:20Z")

</div>

Solved

OK, looks like I can use ConstructObject() to create components.  
Here’s a working example:

```auto

UTextBlock *tester_text = ConstructObject<UTextBlock>(UTextBlock::StaticClass());
tester_text->SetText(FText::FromString(TEXT("Tester Button")));
UButton *tester_button = ConstructObject<UButton>(UButton::StaticClass());
tester_button->AddChild(tester_text);
panel->AddChild(tester_button); // panel is a vertical box instance

```

I used this to modify a newly created instance of UUserWidget which has a vertical box (panel).

Cheers.

---

<div class="post-metadata">

**Author:** ![kamrann](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kamrann](https://forums.unrealengine.com/u/kamrann)\
**Post date:** [July 19, 2015, 9:43pm UTC](https://forums.unrealengine.com/t/how-to-initialize-custom-widgets/35716/6 "2015-07-19T21:43:04Z")

</div>

Cool, yeah I assumed it must be doable in C++, good to know this works.  
By the way, _ConstructObject_ has been deprecated as of 4.8; _NewObject_ should be used instead.
