# Create UMG Button from C++

**URL:** <https://forums.unrealengine.com/t/create-umg-button-from-c/308052>\
**Category:** UI\
**Tags:** unreal-engine, CPP, question, UMG\
**Created:** [February 25, 2015, 11:38am UTC](https://forums.unrealengine.com/t/create-umg-button-from-c/308052 "2015-02-25T11:38:32Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![kshepitzki\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kshepitzki\_1](https://forums.unrealengine.com/u/kshepitzki_1)\
**Post date:** [February 25, 2015, 11:38am UTC](https://forums.unrealengine.com/t/create-umg-button-from-c/308052/1 "2015-02-25T11:38:32Z")

</div>

I am trying to create a UMG button (UButton) inside a verticalBox from C++ and make it appear inside the canvas of the UMG Widget I created as BluePrint.  
I have googled up and down and can’t find a single working example,  
neither for creating a UMG built-in type in C++  
nor for displaying the created widget in the world.

Please help.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_741503d3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_741503d3](https://forums.unrealengine.com/u/anonymous_user_741503d3)\
**Post date:** [February 25, 2015, 12:02pm UTC](https://forums.unrealengine.com/t/create-umg-button-from-c/308052/2 "2015-02-25T12:02:43Z")

</div>

I’ve answered pretty simillar question recently, take a look: [Using a UMG HUD with c++ game mode and player character - UI - Epic Developer Community Forums](https://answers.unrealengine.com/questions/176790/using-a-umg-hud-with-c-game-mode-and-player-charac.html)

---

<div class="post-metadata">

**Author:** ![kshepitzki\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kshepitzki\_1](https://forums.unrealengine.com/u/kshepitzki_1)\
**Post date:** [February 25, 2015, 5:09pm UTC](https://forums.unrealengine.com/t/create-umg-button-from-c/308052/3 "2015-02-25T17:09:21Z")

</div>

Thanks for replying.  
I am not trying to extend UUserWidget. I want to instantiate from C++ instances of the the existing UE4 UButton and UVerticalBox classes and make them show on my Widget (extended from UUserWidget) which is already attached to a view-port.  
CreateWidget does not work with UButton.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_741503d3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_741503d3](https://forums.unrealengine.com/u/anonymous_user_741503d3)\
**Post date:** [February 25, 2015, 5:23pm UTC](https://forums.unrealengine.com/t/create-umg-button-from-c/308052/4 "2015-02-25T17:23:07Z")

</div>

I believe it’s still the same thing. You just need to use UUserWidget instead of YourCustomWidget. Buttons are not widgets on their own, they are components. So you need to create a widget first and then add buttons, verticalBoxes or whatever else to them. You can also check out this tutorial by Rama: [A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums](https://wiki.unrealengine.com/UMG,_Create_Scrollable_List_of_Clickable_Buttons_From_Dynamic_Array)

---

<div class="post-metadata">

**Author:** ![kshepitzki\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kshepitzki\_1](https://forums.unrealengine.com/u/kshepitzki_1)\
**Post date:** [February 25, 2015, 5:27pm UTC](https://forums.unrealengine.com/t/create-umg-button-from-c/308052/5 "2015-02-25T17:27:23Z")

</div>

Rama is not doing it from C++, and if Buttons are components would you be kind enough to show me how to create a button from C++ and display it on my UUserWidget because I don’t have a clue.

---

<div class="post-metadata">

**Author:** ![robbiecooper](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@robbiecooper](https://forums.unrealengine.com/u/robbiecooper)\
**Post date:** [February 25, 2015, 6:00pm UTC](https://forums.unrealengine.com/t/create-umg-button-from-c/308052/6 "2015-02-25T18:00:57Z")

</div>

You can have the C++ create the content of the button and what it does, but I think the rest of it has to be in blueprints.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_82a61a3e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_82a61a3e](https://forums.unrealengine.com/u/anonymous_user_82a61a3e)\
**Post date:** [March 1, 2015, 2:03pm UTC](https://forums.unrealengine.com/t/create-umg-button-from-c/308052/7 "2015-03-01T14:03:46Z")

</div>

Hi,

you can create a widget in code with the global namespace “CreateWidget” call.

I do not know, if CreateWidget works with a native UButton, but you could also workaround this, by creating a UUserwidget  
out of a widget-blueprint which has a nested UButton in it, and adding this widget to your parent.

```
	UUserWidget* pButtonWidget = CreateWidget<UUserWidget>(GetWorld(), ButtonTemplateWidget);
	pButtonWidget ->AddToViewport();

	pMyParentWidget->AddChild(pButtonWidget);

```

The UButton from your created UUserWidget instance can be accessed, if you know the name of your button:

UButton\* pButton = (UButton\*)pButtonWidget -\>GetWidgetFromName(FName(“Button”));

The property ButtonTemplateWidget could be declared in the header, so it can be set from the editor if your class is derived from UObject:

```
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = UI)
TSubclassOf<UUserWidget> ButtonTemplateWidget;

```

Hope this helps!

---

<div class="post-metadata">

**Author:** ![MECCABINGO](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MECCABINGO](https://forums.unrealengine.com/u/MECCABINGO)\
**Post date:** [May 22, 2015, 11:08am UTC](https://forums.unrealengine.com/t/create-umg-button-from-c/308052/8 "2015-05-22T11:08:35Z")

</div>

This worked well for me when I wanted to add it to an existing Panel

```
void UMyGameInstance::MyPopulate(UPanelWidget *panel) {
    UTextBlock *tstText = ConstructObject<UTextBlock>(UTextBlock::StaticClass(), this);
    tstText->SetText(FText::FromString(TEXT("Test Button")));
    UButton *tstBtn = ConstructObject<UButton>(UButton::StaticClass(), this);
    tstBtn->AddChild (tstText);
    panel->AddChild(tstBtn);
}
```

---

<div class="post-metadata">

**Author:** ![FrankForce](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@FrankForce](https://forums.unrealengine.com/u/FrankForce)\
**Post date:** [March 30, 2016, 11:45pm UTC](https://forums.unrealengine.com/t/create-umg-button-from-c/308052/9 "2016-03-30T23:45:11Z")

</div>

I was having trouble figuring out how to do this in my widget I inherited from UUserWidget. Eventually figured out I needed to do a few casts, like casting GetRootWidget to a panel widget so I could add a child, and casting the result of that to a UCanvasPanelSlot instead of a normal UPanelSlot so I could set the position and scale information.

Here’s a code snipped in hopes that it helps someone…

```
// create image and set its position
UImage* image = WidgetTree->ConstructWidget<UImage>(UImage::StaticClass());
image->SetBrushFromTexture(IndicatorTexture);
image->SetColorAndOpacity(FLinearColor(0, 0, 0, 0));

// add it to child of root
UPanelWidget* root = Cast<UPanelWidget>(GetRootWidget());
UPanelSlot* slot = root->AddChild(image);
UCanvasPanelSlot* canvasSlot = Cast<UCanvasPanelSlot>(slot);

// set position and stuff
canvasSlot->SetAnchors(FAnchors(0.5f, 0.5f));
canvasSlot->SetAlignment(FVector2D(0.5f, 0.5f));
canvasSlot->SetOffsets(FMargin(0, 0, IndicatorSize, IndicatorSize));
```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_af11fb2b](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_af11fb2b](https://forums.unrealengine.com/u/anonymous_user_af11fb2b)\
**Post date:** [April 19, 2016, 1:56pm UTC](https://forums.unrealengine.com/t/create-umg-button-from-c/308052/10 "2016-04-19T13:56:07Z")

</div>

If you perform AddChild on a parent, you don’t have to call AddToViewport on widget. 4.10.4

---

<div class="post-metadata">

**Author:** ![Pelangi](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Pelangi](https://forums.unrealengine.com/u/Pelangi)\
**Post date:** [August 16, 2016, 12:56pm UTC](https://forums.unrealengine.com/t/create-umg-button-from-c/308052/11 "2016-08-16T12:56:37Z")

</div>

Hey ThisGuy5234, that not solving the problem because `ButtonTemplateWidget` variable must be assigned an actual Widget Blueprint in the editor. The main concern is how to do it without a single interference from Widget Blueprint?

---

<div class="post-metadata">

**Author:** ![Pelangi](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Pelangi](https://forums.unrealengine.com/u/Pelangi)\
**Post date:** [August 16, 2016, 12:58pm UTC](https://forums.unrealengine.com/t/create-umg-button-from-c/308052/12 "2016-08-16T12:58:25Z")

</div>

It does not fully answer the question. From that source code, how do you add it to a custom widget (empty) derived from UUserWidget object?

What must be available in the header file and the CPP file?  
That’s the answer everybody is looking for in this page.

---

<div class="post-metadata">

**Author:** ![Sneirox](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sneirox](https://forums.unrealengine.com/u/Sneirox)\
**Post date:** [November 18, 2016, 7:49pm UTC](https://forums.unrealengine.com/t/create-umg-button-from-c/308052/13 "2016-11-18T19:49:48Z")

</div>

LOL  
It’s impossible.

---

<div class="post-metadata">

**Author:** ![ndelchen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ndelchen](https://forums.unrealengine.com/u/ndelchen)\
**Post date:** [January 18, 2018, 1:24pm UTC](https://forums.unrealengine.com/t/create-umg-button-from-c/308052/14 "2018-01-18T13:24:38Z")

</div>

Hey this works well, thanks for the info. However, I have a question: I would like to alter the image (size , position etc.) in the blueprint editor after creating it in c++. Do I have to make the variables uproperties or why is this not working?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_fc3d177f](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_fc3d177f](https://forums.unrealengine.com/u/anonymous_user_fc3d177f)\
**Post date:** [February 22, 2019, 10:52pm UTC](https://forums.unrealengine.com/t/create-umg-button-from-c/308052/15 "2019-02-22T22:52:16Z")

</div>

Are you doing this in the native constructor? Every possible call to GetRootWidget() returns null in my code and I don’t understand why.

---

<div class="post-metadata">

**Author:** ![LuisAlaguna](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/luisalaguna/32/570408_2.png) [@LuisAlaguna](https://forums.unrealengine.com/u/LuisAlaguna)\
**Post date:** [May 31, 2019, 3:37pm UTC](https://forums.unrealengine.com/t/create-umg-button-from-c/308052/16 "2019-05-31T15:37:13Z")

</div>

how can i get that WidgetTree inside of a UUserWidget class?
