Skip to content

Instantly share code, notes, and snippets.

@aquanox
Last active July 10, 2025 14:57
Show Gist options
  • Select an option

  • Save aquanox/ea39e33f2ece20f50dcf818c83524a26 to your computer and use it in GitHub Desktop.

Select an option

Save aquanox/ea39e33f2ece20f50dcf818c83524a26 to your computer and use it in GitHub Desktop.
SSearchableComboBox with struct content

I wanted to use SSearchableComboBox for the nice searchable ui but it was tied to TSharedPtr<FString>

I did not want to copy the entire thing but C++ came for the rescue, sharing the cursed knowledge.

// The struct. FString has to be first member
struct FMyItemStruct 
{  
    FString DisplayString;
    // ... other members ... 
}

// The container for combobox populated with MakeShared<FMyItemStruct>(...)
TArray<TSharedPtr<FMyItemStruct>> ComboListItems;

// Combobox itself
SAssignNew(SSearchableComboBox)
    .Content()
    [
        SNew(STextBlock).Font(Font).Text(MakeAttributeSP(this, &ThisClass::GetDisplayText))
    ]
    .OptionsSource( reinterpret_cast<const TArray<TSharedPtr<FString>>*>(&ComboItemList)  )
    .OnGenerateWidget(this, &ThisClass::OnGenerateComboWidget)
    .OnSelectionChanged(this, &ThisClass::OnSelectionChangedInternal)
    .SearchVisibility(EVisibility::Visible)


  // reinterpret_cast will give access to entire struct at element pointer
TSharedRef<SWidget> OnGenerateComboWidget(TSharedPtr<FString> InElement)
{
    auto* TheItem = reinterpret_cast<const FMyItemStruct*>(InElement.Get());
}

void OnSelectionChangedInternal(TSharedPtr<FString> Selected, ESelectInfo::Type)
{
        auto* TheItem = reinterpret_cast<const FMyItemStruct*>(InElement.Get());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment