Announcement

Collapse
No announcement yet.

How to delete SCompoundWidget in OnClicked_Lambda?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • started a topic How to delete SCompoundWidget in OnClicked_Lambda?

    How to delete SCompoundWidget in OnClicked_Lambda?

    I create my own SCompoundWidget.
    In Construct function I added child widget - close button:

    SNew(SBorder)
    .Content()
    [
    SNew(SButton )
    .ForegroundColor( FSlateColor::UseForeground() )
    .OnClicked_Lambda([]()
    {
    // I NEED CLOSE HERE

    return FReply::Handled();
    })
    ]


    How I can close (remove, not hide) widget in this lambda?




  • replied
    Is the problem that you need a reference to the an object you are creating? If so, maybe this example from SAnimationEditorViewport.cpp will give you an idea. Notice how the containing widget is created before the widget with the lambda that dismisses it. This allows the dismiss widget's lambda to capture the containing widget.

    Also notice how the lambda captures the pointer to the widget using a TWeakPtr (to prevent reference cycles).

    Code:
    TSharedPtr<SHorizontalBox> BodyBox = nullptr;
    
    ViewportNotificationsContainer->AddSlot()
    .HAlign(HAlign_Right)
    .AutoHeight()
    .Padding(MakeAttributeLambda(GetPadding))
    [
      SAssignNew(ContainingWidget, SBorder)
      .Visibility_Lambda(GetVisibility)
      .BorderImage_Lambda(GetBrushForSeverity)
      [
        SAssignNew(BodyBox, SHorizontalBox)
        +SHorizontalBox::Slot()
        .FillWidth(1.0f)
        [
          InNotificationWidget
        ]
      ]
    ];
    
    TWeakPtr<SWidget> WeakContainingWidget = ContainingWidget;
    auto DismissNotification = [this, WeakContainingWidget]()
    {
      if(WeakContainingWidget.IsValid())
      {
        RemoveNotification(WeakContainingWidget.Pin().ToSharedRef());
      }
    
      return FReply::Handled();
    };
    
    auto GetDismissButtonVisibility = [InCanBeDismissed]()
    {
      return InCanBeDismissed.Get() ? EVisibility::Visible : EVisibility::Collapsed;
    };
    
    // add dismiss button
    BodyBox->InsertSlot(0)
    .AutoWidth()
    .HAlign(HAlign_Center)
    .VAlign(VAlign_Top)
    [
      SNew(SButton)
      .Visibility_Lambda(GetDismissButtonVisibility)
      .ButtonStyle(FEditorStyle::Get(), "AnimViewport.Notification.CloseButton")
      .ToolTipText(LOCTEXT("DismissNotificationToolTip", "Dismiss this notification."))
      .OnClicked_Lambda(DismissNotification)
    ];
    Last edited by drichardson; 02-12-2019, 03:16 PM. Reason: include missing code and improve readability

    Leave a comment:

Working...
X