Lambda as slate delegate

Is it possible to use a lambda as a slate delegate for generating items in a SListView?

eg

SNew(SListView<TSharedRef<FString> >)
    .ListItemsSource(&SessionNames)
    .OnGenerateRow([](TSharedRef<FString> string)->TSharedRef < ITableRow > {return SNew(STextBlock).Text((*string)); })

I’m guessing that if it is possible I’d have to wrap this somehow as an actual delegate, as I get an error about being unable to convert from the lambda to a RetVal_TwoParams delegate. Is this the case, and if so how would I go about doing so?

Does RetVal_TwoParams mean that your lambda should accept two arguments?

According to the comments in SListView.h:

* A sample implementation of OnGenerateWidgetForList would simply return a TextBlock with the corresponding text:
 *
 * TSharedRef<ITableRow> OnGenerateWidgetForList( FString* InItem )
 * {
 *     return SNew(STextBlock).Text( (*InItem) )
 * }

Bearing this in mind, I shouldn’t need two parameters in the lambda, because the sample delegate function provided there doesn’t take two.

Thanks for the clarification, Jamie. Will give it a shot and see how things go :slight_smile:

That comment is wrong (perhaps old), I’ve updated it.

FOnGenerateRow needs two parameters, your item, and the owner table. To update the example you used above, this would be:

 TSharedRef<ITableRow> OnGenerateWidgetForList( FString* InItem, const TSharedRef<STableViewBase>& OwnerTable )
 {
     return SNew(STextBlock).Text( (*InItem) )
 }

You should be able to use a lambda providing they don’t capture anything. Our delegates don’t really support lambdas, but they do support static function pointers, and a stateless lambda will convert to a function pointer.

A little bit late, but you can now wrap a lambda in a TAttribute similar to this (just change types to whatever you need):

SNew(STextBlock)
.Text(TAttribute<FText>::Create(TAttribute<FText>::FGetter::CreateLambda([&](){ return Something.GetText(); })))

Or even better:

SNew(STextBlock)
 .Text_Lambda([&](){ return Something.GetText(); })
1 Like

Tried it, that works. Looked for this for a really long time, wish I had found this answer earlier :wink: