# Create widget in pure C++

**URL:** <https://forums.unrealengine.com/t/create-widget-in-pure-c/367306>\
**Category:** UI\
**Tags:** create, unreal-engine, Widget, question, CPP\
**Created:** [August 16, 2016, 1:20pm UTC](https://forums.unrealengine.com/t/create-widget-in-pure-c/367306 "2016-08-16T13:20:27Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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 16, 2016, 4:05pm UTC](https://forums.unrealengine.com/t/create-widget-in-pure-c/367306/2 "2016-08-16T16:05:45Z")

</div>

First you create a widget that extends UUserWidget.

```
class UMyUserWidget : public UUserWidget

```

Next you override the NativeConstruct method.

```
virtual void NativeConstruct() override;

```

In your .cpp file you implement NativeConstruct.

```
void UMyUserWidget::NativeConstruct()
{
    Super::NativeConstruct();

    // Bind delegates here.
}

```

In your PlayerController class create and display the widget.

```
UserInterface = CreateWidget<UMyUserWidget>(this, UMyUserWidget::StaticClass());
FInputModeGameAndUI Mode;
Mode.SetLockMouseToViewport(true);
Mode.SetHideCursorDuringCapture(false);
SetInputMode(Mode);
UserInterface->AddToViewport(9999); // Z-order, this just makes it render on the very top.

```

Now that you have created your base widget in C++ you can start adding components to it. It is extremely important to note that **widget components are also widgets**. This means you create them in the exact same way you create your UUserWidget.

In your header file define a widget type. In this case I’ll define a UButton.

```
UPROPERTY()
UButton* ExampleButton;

```

In your CPP file you will instantiate that button.

```
UPanelWidget* RootWidget = Cast<UPanelWidget>(GetRootWidget());

ExampleButton = WidgetTree->ConstructWidget<UButton>(UButton::StaticClass(), TEXT("MyButtonName")); // The second parameter is the name and is optional.
ExampleButton->OnClicked.AddDynamic(this, &MyUserWidget::OnButtonClicked); // Example click binding.
RootWidget->AddChild(ExampleButton);

```

There are, of course, more steps in this like setting the padding, color, and other properties. It is best that you give your widgets names so you can find them later using using WidgetTree-\>FindWidget(TEXT(“WIDGET\_NAME”)).

> **[UUserWidget](https://docs.unrealengine.com/5.1/en-US/API/Runtime/UMG/Blueprint/UUserWidget/)**
>
> The user widget is extensible by users through the WidgetBlueprint.

> **[UWidgetTree](https://docs.unrealengine.com/5.1/en-US/API/Runtime/UMG/Blueprint/UWidgetTree/)**
>
> The widget tree manages the collection of widgets in a blueprint widget.

> **[UButton](https://docs.unrealengine.com/5.1/en-US/API/Runtime/UMG/Components/UButton/)**
>
> The button is a click-able primitive widget to enable basic interaction, you can place any other widget inside a button to make a more complex and interesting click-able element in your UI.

---

_[View the full topic](https://forums.unrealengine.com/t/create-widget-in-pure-c/367306)._
