Create UMG Button from C++

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.

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

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.

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

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.

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.

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!

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);
}

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));

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

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?

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.

LOL
It’s impossible.

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?

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

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