Download

UButton OnClicked() does not fired, when combined Slate and UMG

I can’t receive OnClicked event from my button that I have created at UMG.
I have created two classes:

  1. SMyConfirmationDialog : public SCompoundWidget
  2. UConfirmationDialogWidget : public UUserWidget

I have added my UMG class to slate using following code:


void SMyConfirmationDialog::Construct(const FArguments& InArgs)
{
    ...

    if (!confirmationDialog)
    {
        UBlueprint* tmpWidget = LoadObjFromPath<UBlueprint>(TEXT("WidgetBlueprint'/Game/UI/Widgets/ConfirmationDialogWidget.ConfirmationDialogWidget'"));
        if (tmpWidget)
        {
            confirmationDialogBPClass = (UClass*)(tmpWidget->GeneratedClass);
        }

        if (confirmationDialogBPClass)
        {
            confirmationDialog = CreateWidget<UConfirmationDialogWidget>(mGameInstance.Get(), confirmationDialogBPClass);
            if (confirmationDialog)
            {
                confirmationDialog->Setup(this);
            }
        }
    }

    ChildSlot
    .HAlign(HAlign_Fill)
    .VAlign(VAlign_Fill)
    
        // Populate the widget
        // that code call the 'NativeConstruct' function
        confirmationDialog->TakeWidget()
    ];
}

where


//Confirmation dialog widget
UPROPERTY()
TSubclassOf<UConfirmationDialogWidget>    confirmationDialogBPClass;

UPROPERTY()
UConfirmationDialogWidget*    confirmationDialog = nullptr;

I have created a function that must calling in blueprint when I pressed button on UMG form:


UFUNCTION(BlueprintCallable, Category = "Actions")
void OnOKPressed();


(Of course I have created Bluprint class based on UConfirmationDialogWidget class)
In Bluprint editor I have created OnClicked handler and connected it with OnOKPressed, but with no luck
Also I have tried used the following code, but also with no luck:


void UConfirmationDialogWidget::NativeConstruct()
{
    Super::NativeConstruct();
    //
    UButton* btnWidget;
    UWidget* tmpWidget = GetWidgetFromName(TEXT("OkButton")); //"OkButton" - is the correct name, I've checked
    if (tmpWidget)
    {
        btnWidget = Cast<UButton>(tmpWidget);
        if (btnWidget)
        {
            btnWidget->OnClicked.AddDynamic(this, &UConfirmationDialogWidget::OnOKPressed);
        }
    }
}

What I am doing wrong? Maybe it is a bug?
Please Help me with it

I have just solved that problem, but I have no idea why…
Іn the slate you have to add this line in section of SButton


.IsFocusable(false)

or uncheck the “Is Focusable” checkbox in Interaction section of button properties
Someone can explane me why?

Don’t use GetWidgetFromName. Use the meta properties for UPROPERTY instead:



UPROPERTY(meta = (BindWidget))
UButton* MyButton;


Create a UMG Widget with a Button called ‘MyButton’ and expose it as a variable, and it’ll magically be hooked up to that pointer when created.