Creating UI with C++

Hi everyone,

sorry to bother with this, I’m aware that I’m probably doing some stupid mistake.

Basically I want to add on the screen some lines of text (not for debugging) and maybe later on a button too.
I would really like to do this completely with C++.

What I’ve done so far:

  1. I created a custom UserWidget class as follows (I show the cpp file only)

    #include “MyUserWidget.h”
    #include “Blueprint/WidgetTree.h”
    #include “Components/PanelWidget.h”
    #include “Components/TextBlock.h”

    void UMyUserWidget::NativeConstruct()
    {
    Super::NativeConstruct();

    }

    TSharedRef UMyUserWidget::RebuildWidget()
    {
    GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Blue, TEXT(“RebuildWidget() called”));

     TSharedRef<SWidget> Widget = Super::RebuildWidget();
    
     RootWidget = WidgetTree->ConstructWidget<UPanelWidget>(UPanelWidget::StaticClass(), TEXT("RootWidget"));
     //WidgetTree->RootWidget = RootWidget;
    
     //GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Blue, FString::Printf(TEXT("Root: %d, Tree: %d"),bool(RootWidget),bool(WidgetTree)));
    
     if (RootWidget && WidgetTree)
     {
         UIControls = WidgetTree->ConstructWidget<UTextBlock>(UTextBlock::StaticClass());
         UIControls->SetText(FText::FromString("hello"));
         UIControls->SetColorAndOpacity(FLinearColor(209.0f, 0.0f, 0.0f, 1.0f));
         GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Blue, TEXT("ok"));
         RootWidget->AddChild(UIControls);
     }
    
     return Widget;
    

    }

  2. I created a widget inside the BeginPlay function of my player controller class

    UserInterface = CreateWidget(this, UMyUserWidget::StaticClass());
    UserInterface->AddToViewport(999); // Z-order, this just makes it render on the very top.

where UserInterface is defined inside the controller header file:

	UPROPERTY()
	UMyUserWidget* UserInterface;

As it is right now it compiles fine, with no errors, but nothing appears on my screen (except debug lines) when I try to run it in viewport.

Can you spot any mistake? Please tell me if I need to show more code.

Best,

Hi! You write that you want to do it fully with C++. I think you can do all without U* classes (UObject widgets) only with S* classes (pure Slate widgets). Is it some special case for you to use U* classes but willing to got all implementations in C++?

Hi Kehel!
No, actually there is no particular reason. What you see in my code above is just what I could find on the topic around the web. I will check Slate widget. Can you suggest me a tutorial or the basic steps to follow in order to achieve what I wish for?

Thats because you calling TSharedRef Widget = Super::RebuildWidget(); before oyu initate the widgets.

UMG is a blueprint wrapper system for Slate and function you trying to use is to declare Slate widgets to be represented by your UMG widget class.

You can read how to declare slate widgets here:

and overall about slate here:

If you ask me Slate is a lot nicer to use in C++ then UMG (look how slick deceleration syntax looks), you can use Slate on full power (atleast stuff that are not part of Editor code) which UMG not 100% cover, so go straight up declare Slate widgets in that function and use UMG just to host them as you already kind of at this point.

If you go this way, first of all use UWidget instead of UUserWidget, UUserWidget is a UMG composition widget containing composition of multiple UMG widgets, it was not made to control Slate widgets but UMG widgets, while UWidget is a single atomic UMG widget which can contain and control single or composition of Slate widgets, entire Slate composition by UMG system will be visible as single UMG widget, which you be able to place on UMG editor or just place it on the screen same way as you do with UUserWidget. You UMG widget should control entire composition of Slate widgets, should have all control functions.

If you gonna use this widget in blueprint widget editor (place in blueprint widget) make sure to also override SynchronizeProperties function to sync UWidget properties with Slate widgets, or else you wont see changes in widget editor etc. which later you can call in later also in blueprints

Also you don’t need to make UUserWidget to use communicate with your UMG widget via blueprint) as it fully Blueprint compatible (it’s UObject and not to mention you can place it in widget blueprint and talk to it), you only need that class if you plan to make widget blueprint out of it.

1 Like

http://students.ceid.upatras.gr/~vpapadatos/UE4cpp_slate_1.html