Setting Up Widget Elements in Constructor of UUserWidget

I’d like to make a constructor for a UUserWidget in which I add some widget elements like a vertical box to the widget tree that can be edited in the UMG view but still called from C++, as you may add a component to an actor through C++ in the constructor and edit it in the viewport.
This seems like the most natural way to work with UMG from C++, but I’m stumbling all over it!
Is this possible?
How would I do it?

Thanks!

I’ve not yet found a way to do this, so far everything seems to point at can’t be done, but nothing with a staff member confirming that’s the case and whether it’s intended. Here’s another thread on the same question. https://answers.unrealengine.com/questions/521224/request-improve-inheritance-in-userwidget.html

Here’s another threadd with the same question

I have an answer from Unreal!
Special thanks to Cody Albert.

"it sounds like you’re looking for BindWidget, a property metatag that lets you define a UMG widget property in C++ and automatically bind to the instanced created with the same name in the UMG editor. We don’t have much documentation for the tag just yet, but I’ll walk you through it.

On your C++ class which inherits UUserWidget, define UProperties for any widgets you want as follows:

 UPROPERTY(meta = (BindWidget)) 
 UPanelWidget* MyPanel;

Then, when you create a UUserWidget using your derived class in the UMG editor, you’ll create a panel and name it MyPanel, and we’ll automatically bind this c++ property to the created widget behind the scenes. Note that you’ll see a compile error if no widget MyPanel is found or if it’s a different widget class. Alternatively, if you don’t want to see a compile error and want the implementation of the property to be optional, you can declare it as follows:

 UPROPERTY(meta = (BindWidgetOptional)) 
 UPanelWidget* MyOptionalPanel;

Best,

Cody"

2 Likes

I have posted the answer now :slight_smile:

Wow. Thanks for sharing this. Exactly what I needed.