Unreal Engine: How to Add Widgets With C++ at Runtime

Let’s say you have an existing UMG UI which you’ve created in C++ already* (derived from UUserWidget), and you want to add child widgets at runtime. Imagine you have an inventory UI with a container of some kind (I’ll use a vertical box in this example) and you want to add an item at which the player is aiming. You could add a method to your UUserWidget-derived class like this (I’ve used a text block for the example but you can use any widget derived from UWidget):

void UMyUserWidget::AddInventoryItem(FText ItemName)
    auto NewInventoryItemWidget = WidgetTree->ConstructWidget<UImage>();

That’s it!

* Mike Stevanovic has a great tutorial for creating UUserWidgets and adding them to the viewport using C++.