Say I want to change a button’s style after being clicked, how to get a reference to itself in the OnClick
event handler when using SNew
to construct?
Try searching for SharedThis samples in source code:
TSharedPtr<FMyEditorToolkit> MyToolkitRef = SharedThis(this);
void Construct(const FArguments &InArgs, const TSharedPtr<FMyEditorToolkit> & TheRef);
SAssignNew(MySWidgetInstance, SMyCustomEditorWidgetType, MyToolkitRef);
I was originally declared a pointer to it and using SAssignNew
in a function scope, and then capture it in a lambda, but the captured pointer was nullptr when the lambda was called, something like
void f()
{
TSharedPtr<SButton> buttonPtr;
SNew(SVerticalBox)
+ SVerticalBox::Slot()
[
SAssignNew(buttonPtr, SButton)
.OnClick(FOnClick::CreateLambda([buttonPtr](){ /*buttonPtr is null here*/})
//...
]
}
I guess I will just have to make the pointer a class member then.