Slate SComboBox example

Any chance anyone could give a simple example of SComboBox? I’ve checked numerous examples in source code but mine just isn’t ‘dropping down’ when I click it.

#define LOCTEXT_NAMESPACE “SMyCombo”

class SMyCombo : public SCompoundWidget
{
public:
	SLATE_BEGIN_ARGS(SMyCombo)
	{}
	SLATE_END_ARGS()

	typedef TSharedPtr<FString> FComboItemType;
	
	void Construct(const FArguments& InArgs)
	{
		Options.Add(MakeShareable(new FString("Option1")));
		Options.Add(MakeShareable(new FString("Option2")));
		Options.Add(MakeShareable(new FString("LastOption")));

		CurrentItem = Options[0];

		ChildSlot
			[
				SNew(SComboBox<FComboItemType>)
				.OptionsSource(&Options)
				.OnSelectionChanged(this, &SMyCombo::OnSelectionChanged)
				.OnGenerateWidget(this, &SMyCombo::MakeWidgetForOption)
				.InitiallySelectedItem(CurrentItem)
				[
					SNew(STextBlock)
					.Text(this, &SMyCombo::GetCurrentItemLabel)
				]
			];
	}

	TSharedRef<SWidget> MakeWidgetForOption(FComboItemType InOption)
	{
		return SNew(STextBlock).Text(FText::FromString(*InOption));
	}

	void OnSelectionChanged(FComboItemType NewValue, ESelectInfo::Type)
	{
		CurrentItem = NewValue;
	}

	FText GetCurrentItemLabel() const
	{
		if (CurrentItem.IsValid())
		{
			return FText::FromString(*CurrentItem);
		}

		return LOCTEXT("InvalidComboEntryText", "<<Invalid option>>");
	}

	FComboItemType CurrentItem;
	TArray<FComboItemType> Options;
};

#undef LOCTEXT_NAMESPACE
2 Likes

Okay i think I see what ive missed, thank you

That’s a much better and nicer example than the one hidden in the depths of the Testing folder. Thanks!

(Which is Program Files\Epic Games\UE_4.15\Engine\Source\Runtime\AppFramework\Private\Framework\Testing for anyone wondering. Some minor variation will apply.)

nice bro