UUserWidget Parental Problem


Consider a scenario,

  1. I created a new C++ class inherited from UUserWidget (Let us call it A)

  2. Then I created a new widget BP.(Let us call it B)

  3. Then I made A (By going to class settings), the parent of B. (so B is a child of A)

  4. After that I started working on B(widget BP)
    i) Added a UImage and named it “TestImage”

  5. Inside the class A, I tried to get TestImage
    UImage* Image = (UImage*)GetWidgetFromName(“TestImage”);

Here I am able to get reference of the Image in B from A. And I can manipulate it in any way I want.

How is this possible?
How can I access a widget in child(B) from parent(A)?

It seems like the exact opposite of inheritance.

I’m confused, is B or A a child of the C++ class or not? If not then that code will always fail.

Additionally, there are special UPROPERTY tags you can use to bind to widget components without heving to find them in C++ (GetWidgetFromName is Editor Only and will fail in a package build). I’ve shown how to use them here:

B is the child of A and A is the child of UUserWidget C++ class.

The problem here for me is inheritance.
It seems like it is against the rule of inheritance.
You see, A is the parent of B, but i am able to refer to widgets in B from A and manipulate it in any way i want.

Thank you for your time and reply.
What could be the reason? Is there any other concept related to inheritance?

My first though is that ‘GetWidgetFromName’ probably just doesn’t work the way you think it does, and it’s failing.

Also, I don’t think that the C-Style cast you’re using would work anyway. You probably need to use unreal’s cast function:

UImage* TestImagePtr = Cast<UImage>(GetWidgetFromName(TEXT(""TestImage")));