x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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.

Product Version: Not Selected
Tags:
more ▼

asked Feb 25 '15 at 11:38 AM in C++ Programming

avatar image

kshepitzki
31 3 5 5

avatar image IanBreeg Feb 25 '15 at 12:02 PM
avatar image kshepitzki Feb 25 '15 at 05:09 PM

Ian 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.

avatar image IanBreeg Feb 25 '15 at 05:23 PM

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: https://wiki.unrealengine.com/UMG,_Create_Scrollable_List_of_Clickable_Buttons_From_Dynamic_Array

avatar image kshepitzki Feb 25 '15 at 05:27 PM

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.

avatar image robbiecooper Feb 25 '15 at 06:00 PM

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.

avatar image Maslobojik Nov 18 '16 at 07:49 PM

LOL It's impossible.

(comments are locked)
10|2000 characters needed characters left

3 answers: sort voted first

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

answered Mar 30 '16 at 11:45 PM

avatar image

FrankForce
36 4 8

avatar image ndelchen Jan 18 '18 at 01:24 PM

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?

avatar image Noah P Johnson Feb 22 '19 at 10:52 PM

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

(comments are locked)
10|2000 characters needed characters left

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

answered May 22 '15 at 11:08 AM

avatar image

MECCABINGO
61 1 1 6

avatar image Pelangi Aug 16 '16 at 12:58 PM

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.

(comments are locked)
10|2000 characters needed characters left

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!

more ▼

answered Mar 01 '15 at 02:03 PM

avatar image

ThisGuy5234
201 9 13 24

avatar image Karlsson600 Apr 19 '16 at 01:56 PM

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

avatar image Pelangi Aug 16 '16 at 12:56 PM

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?

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question