How to receive OnMouseEnter for a Slate menu item? [Solved]

How to know when the mouse enters/exits a slate menu item created using FMenuBuilder::AddMenuEntry?
I want to do something when them mouse pointer hovers over the menu item and the menu item becomes highlighted.

OnMouseEnter() is virtual, you can override on your Slate class.

Wow, quick answer!

I need to create my own Widget for that? Because now I’m simply using the AddMenuEntry with FText as argument, this one:

void FBaseMenuBuilder::AddMenuEntry( const TAttribute<FText>& InLabel, const TAttribute<FText>& InToolTip, const FSlateIcon& InIcon, const FUIAction& InAction, FName InExtensionHook, const EUserInterfaceActionType::Type UserInterfaceActionType, FName InTutorialHighlightName )

In that case I don’t know if it’s possible, through MenuBuilder, since there you cannot provide your own Widget class.

Thank you, I think I will make my own widget for the menu item and put the OnMouseEnter there.

This is the code for OnMouseEnter and OnMouseLeve on a menu item, in case somebody need it in the future.
The menu item widget itself is a simplified version of one found in SMenuEntryBlock::BuildMenuEntryWidget, without icon and checkbox.
You don’t need to implement a custom widget, use handlers provided by SWidget.


                    
FText EntryName = ....... ;

TSharedPtr< SWidget > ButtonContent =
SNew(SHorizontalBox)

+ SHorizontalBox::Slot()
.FillWidth(1.0f)
.Padding(FMargin(2, 0, 6, 0))
.VAlign(VAlign_Center)

    SNew(STextBlock)
    .TextStyle(FEditorStyle::Get(), FEditorStyle::Join(StyleName, ".Label"))
    .Text(EntryName)
];

ButtonContent.Get()->SetOnMouseEnter(FNoReplyPointerEventHandler::CreateLambda(
    [EntryName](const FGeometry& G, const FPointerEvent&E) {
        UE_LOG(LogTemp, Log, TEXT("MOUSE ENTER: %s"),*EntryName.ToString());
    }
));

ButtonContent.Get()->SetOnMouseLeave(FSimpleNoReplyPointerEventHandler::CreateLambda(
    [EntryName](const FPointerEvent&E) {
        UE_LOG(LogTemp, Log, TEXT("MOUSE LEAVE: %s"), *EntryName.ToString());
    }
));

MenuBuilder.AddMenuEntry(
    FUIAction(
        FExecuteAction::CreateLambda(]() {
            //Menu item clicked, do something
        })
    ),
    ButtonContent.ToSharedRef()                                
);


3 Likes

Oh sure… I totally forgot about the ->SetXXX() functions on Slate widgets lol

@scha, thank you so much! Saved me huge amount of time

1 Like