Access UMG Widgets Properties from C++

Does anyone know how to read/write TextBlock, Button, Image position (X, Y) and size (X, Y) from C++?

Can you point me to the documentation?

The best I have found so far is:



        UTextBlock* someItem = NewObject<UTextBlock>(this, UTextBlock::StaticClass());
        someItem->SetText(FText::FromString(TEXT("ABC")));
        someItem->Font.Size = 24;
        someItem->SetColorAndOpacity(FSlateColor(FLinearColor(1.0f, 1.0f, 1.0f, 1.0f)));
        someItem->Justification = ETextJustify::Left;
        someItem->Margin = FMargin(0.0f);
        someItem->MinDesiredWidth = 200.0f;
        someItem->SetJustification(ETextJustify::Left);
        someItem->SetRenderTranslation(FVector2D(0.0, 0.0f));


Those are properties of a canvas panel slot, not the widget itself.

I haven’t tried it, but I’d guess this would work, assuming of course that the widget is placed on a canvas:


UCanvasPanelSlot* CanvasSlot = Cast< UCanvasPanelSlot >(MyWidget->Slot);
CanvasSlot->GetPosition()/GetSize()

2 Likes

Thank you Kamrann.

I suspected as much with the Slot member but without the Cast it doesn’t list the Get/Set functions for Size/Position. The UTextBlock Slot member is of type UPanelSlot*. When casted as you listed above I now get an Access Violation in the Canvas code. Most likely either it’s a bad cast or the object hasn’t been created yet. If memory serves me correctly there’s a function that can be called that forces the widget to fully create itself but I can’t seem to find it now.

Do you have any other suggestions you can share?

Not really without seeing more code showing the context in which you’re trying to do it.
Best bet would be to put a breakpoint in the debugger at the line of the cast, and check on the value of MyWidget->Slot that you are about to cast. The debugger should show you if it’s null, and if not, what kind of slot it is.

Well, thanks to you Kamrann I have it working :slight_smile:

It’s a matter of order of operations. Creating the widgets alone doesn’t give them the inheritance to any future class that it may be appended to :smiley: Basically, casting to Canvas anything without being added to the canvas won’t work, hence, the Access Violation. I ended up with the following:



        UTextBlock* someItem = NewObject<UTextBlock>(this, UTextBlock::StaticClass());
        panel->AddChild(someItem);
        Cast<UCanvasPanelSlot>(someItem->Slot)->SetPosition(FVector2D( x, y));
        Cast<UCanvasPanelSlot>(someItem->Slot)->SetSize(FVector2D( w, h));


And for completeness sake. . .I’ve been running with Visual Studio.