# How to construct UMG widget in C++?

**URL:** <https://forums.unrealengine.com/t/how-to-construct-umg-widget-in-c/384615>\
**Category:** UI\
**Tags:** unreal-engine, question, UMG\
**Created:** [February 11, 2017, 11:24am UTC](https://forums.unrealengine.com/t/how-to-construct-umg-widget-in-c/384615 "2017-02-11T11:24:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbprofi](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@newbprofi](https://forums.unrealengine.com/u/newbprofi)\
**Post date:** [February 11, 2017, 11:24am UTC](https://forums.unrealengine.com/t/how-to-construct-umg-widget-in-c/384615/1 "2017-02-11T11:24:59Z")

</div>

I am trying to construct UMG widget in C++ and create simple button inside of it:

```
void UMyUserWidget::NativeConstruct()
{
	Super::NativeConstruct();
	
	auto MyCanvas = WidgetTree->ConstructWidget<UCanvasPanel>(UCanvasPanel::StaticClass());
	auto MyButton = NewObject<UButton>(this);
	MyCanvas->AddChildToCanvas(MyButton);
	WidgetTree->Modify();
	WidgetTree->RootWidget = MyCanvas;
}

```

After adding this widget to viewport, this method is called, but i dont see any button on the screen.

---

<div class="post-metadata">

**Author:** ![newbprofi](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@newbprofi](https://forums.unrealengine.com/u/newbprofi)\
**Post date:** [February 11, 2017, 12:20pm UTC](https://forums.unrealengine.com/t/how-to-construct-umg-widget-in-c/384615/2 "2017-02-11T12:20:34Z")

</div>

I got my button visible on screen by moving this code from NativeConstruct to Initialize function. But after recompile i am getting CanvasPanel is not packaged exception and crash on hot-reload.

---

<div class="post-metadata">

**Author:** ![newbprofi](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@newbprofi](https://forums.unrealengine.com/u/newbprofi)\
**Post date:** [February 11, 2017, 1:28pm UTC](https://forums.unrealengine.com/t/how-to-construct-umg-widget-in-c/384615/3 "2017-02-11T13:28:52Z")

</div>

void UMyUserWidget::Initialize()  
{  
Super::Initialize();

```
// Initialize called after hot-reload with WidgetTree = NULL, skip this unexpected call, or you will get CanvasPanel is not packaged error.
if(WidgetTree)
{
     auto MyCanvas = WidgetTree->ConstructWidget<UCanvasPanel>(UCanvasPanel::StaticClass());
     auto MyButton = WidgetTree->ConstructWidget<UButton>(UButton::StaticClass());
     MyCanvas->AddChildToCanvas(MyButton);
     WidgetTree->RootWidget = MyCanvas;
}

 }

```

Well, now i got UMG widget constructed and visible, but i dont know yet how to do that AFTER Initialize call, for instance inside NativeConstruct function, which is called after Initialize.

---

<div class="post-metadata">

**Author:** ![newbprofi](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@newbprofi](https://forums.unrealengine.com/u/newbprofi)\
**Post date:** [February 11, 2017, 2:00pm UTC](https://forums.unrealengine.com/t/how-to-construct-umg-widget-in-c/384615/4 "2017-02-11T14:00:39Z")

</div>

I’ve got it working dinamically, but i still have to set WidgetTree-\>RootWidget = MyCanvas inside Initialize function:

```
             bool UMyUserWidget::Initialize()
              {
                  bool b = Super::Initialize();
    
    // more correct way to avoid error. than in original post.
            if(!HasAnyFlags(RF_ClassDefaultObject) )
             {
// root have to be initialized in Initialize function, otherwise it will not work, donno exactly why.
                  MyCanvas = WidgetTree->ConstructWidget<UCanvasPanel>();
                  WidgetTree->RootWidget = MyCanvas;
             }
        
        return b;
              }
            
              void UMyUserWidget::NativeConstruct()
              {
                  Super::NativeConstruct();
             
                  auto MyButton = WidgetTree->ConstructWidget<UButton>();
                  MyCanvas->AddChildToCanvas(MyButton);
              }
```

---

<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:48pm UTC](https://forums.unrealengine.com/t/how-to-construct-umg-widget-in-c/384615/5 "2019-05-31T15:48:46Z")

</div>

Where is the WidgetTree?

---

<div class="post-metadata">

**Author:** ![Rominitch1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rominitch1/32/559528_2.png) [@Rominitch1](https://forums.unrealengine.com/u/Rominitch1)\
**Post date:** [September 1, 2022, 4:17pm UTC](https://forums.unrealengine.com/t/how-to-construct-umg-widget-in-c/384615/6 "2022-09-01T16:17:00Z")

</div>

> [@LuisAlaguna](#):
>
> Where is the WidgetTree?

#include “Blueprint/WidgetTree.h”

According to [UWidgetTree | Unreal Engine Documentation](https://docs.unrealengine.com/5.0/en-US/API/Runtime/UMG/Blueprint/UWidgetTree/)

---

<div class="post-metadata">

**Author:** ![Meechew](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Meechew](https://forums.unrealengine.com/u/Meechew)\
**Post date:** [January 13, 2023, 9:43pm UTC](https://forums.unrealengine.com/t/how-to-construct-umg-widget-in-c/384615/7 "2023-01-13T21:43:39Z")

</div>

I do not know about him, but I am looking how to initialize the WidgetTree.
