Slate SListView item source not being accepted.

I’m trying to get a basic UI working using Slate, unfortunately I’m still fairly inexperienced in C++ so I might be missing something really obvious. Here’s the code in my widget cpp file:

void SMyGameSlateHUDWidget::Construct(const FArguments& InArgs)
{
	OwnerHUD = InArgs._OwnerHUD;

	TArray Items = GetListOfItems();

	ChildSlot
	.VAlign(VAlign_Fill)
	.HAlign(HAlign_Fill)
	[
			SNew(SOverlay)
			+SOverlay::Slot()
			.VAlign(VAlign_Bottom)
			.HAlign(HAlign_Center)
			.Padding(TAttribute(this, &SMyGameSlateHUDWidget::GetPadding))
			[
				SNew(STextBlock)
				.ShadowColorAndOpacity(FLinearColor::Black)
				.ColorAndOpacity(FLinearColor::Red)
				.ShadowOffset(FIntPoint(-1,1))
				.Text(this, &SMyGameSlateHUDWidget::GetSomeString)
				
			]
			+SOverlay::Slot()
			.VAlign(VAlign_Center)
			.HAlign(HAlign_Left)
			[
				
					SNew(SListView)
					.ItemHeight(24)
					.ListItemsSource(&Items)
					.HeaderRow
					(
						SNew(SHeaderRow)
						+ SHeaderRow::Column("Name")
						[
							SNew(SBorder)
							.Padding(5)
							.Content()
							[
								SNew(STextBlock)
								.Text(FString(TEXT("Name")))
							]
						]
						+ SHeaderRow::Column("Number") .DefaultLabel(FString(TEXT("Number")))
						+ SHeaderRow::Column("TextField") .DefaultLabel(FString(TEXT("Text Field")))
						+ SHeaderRow::Column("TextBlock") .DefaultLabel(FString(TEXT("Text Block")))
						+ SHeaderRow::Column("AddChild") .DefaultLabel(FString(TEXT("Add Child")))
					)

				
			]
	];
		
}

TArray SMyGameSlateHUDWidget::GetListOfItems() const
{
	FString temp = FString(TEXT("TestText"));
	TArray Items;
	Items[0] = &temp;

	return Items;
}

The compiler throws this error:

c:\program files\rocket\engine\source\runtime\slate\public\TableView/TableViewTypeTraits.h(215): error C2664: 'FReferenceCollector::AddReferencedObject' : cannot convert parameter 1 from 'FString *' to 'const UObject *'

With a reference later on to the error being on the SNew(SListView) line.

I don’t understand why, since it’s the same as what the example code shows. What am I missing?

Thanks,

Any change in behavior if you use a Shared Ptr?

SNew(SListView<    TSharedPtr   >)

instead of

SNew(SListView)

:slight_smile:

Rama

Well, it compiles now, but when I start it it gives me:

C:\Program Files\Rocket\Engine\Source\Runtime\Core\Public\Containers\Array.h(517): Ensure condition failed: (Index != 0) | (ArrayNum != 0)

I’m guessing something’s wrong with the way I create my array:

void SMyGameSlateHUDWidget::Construct(const FArguments& InArgs)
{
	OwnerHUD = InArgs._OwnerHUD;

	TArray> Items = GetListOfItems();
	

	ChildSlot
	.VAlign(VAlign_Fill)
	.HAlign(HAlign_Fill)
	[
			SNew(SOverlay)
			+SOverlay::Slot()
			.VAlign(VAlign_Bottom)
			.HAlign(HAlign_Center)
			.Padding(TAttribute(this, &SMyGameSlateHUDWidget::GetPadding))
			[
				SNew(STextBlock)
				.ShadowColorAndOpacity(FLinearColor::Black)
				.ColorAndOpacity(FLinearColor::Red)
				.ShadowOffset(FIntPoint(-1,1))
				.Text(this, &SMyGameSlateHUDWidget::GetSomeString)
				
			]
			+SOverlay::Slot()
			.VAlign(VAlign_Center)
			.HAlign(HAlign_Left)
			[
				
					SNew(SListView>)
					.ItemHeight(24)
					.ListItemsSource(&Items)
					.HeaderRow
					(
						SNew(SHeaderRow)
						+ SHeaderRow::Column("Name")
						[
							SNew(SBorder)
							.Padding(5)
							.Content()
							[
								SNew(STextBlock)
								.Text(FString(TEXT("Name")))
							]
						]
						+ SHeaderRow::Column("Number") .DefaultLabel(FString(TEXT("Number")))
						+ SHeaderRow::Column("TextField") .DefaultLabel(FString(TEXT("Text Field")))
						+ SHeaderRow::Column("TextBlock") .DefaultLabel(FString(TEXT("Text Block")))
						+ SHeaderRow::Column("AddChild") .DefaultLabel(FString(TEXT("Add Child")))
					)

				
			]
	];
		
}

TArray> SMyGameSlateHUDWidget::GetListOfItems() const
{
	FString temp = FString(TEXT("TestText"));
	TSharedPtr temp2 = TSharedPtr(&temp);
	TArray> Items;
	Items[0] = temp2;

	return Items;
}

Hi there!

I believe you have an error here:

TArray> Items;
    Items[0] = temp2;

in

TArray> SMyGameSlateHUDWidget::GetListOfItems() const
{
    FString temp = FString(TEXT("TestText"));
    TSharedPtr temp2 = TSharedPtr(&temp);
    TArray> Items;
    Items[0] = temp2;

    return Items;
}

you are making an array, and then trying to access element 0, when no elements yet exist, so you are going out of the bounds of the array.

#Using .Add()

To add an item to a new array, you need to use .Add

TArray> Items;
Items.Add(temp2);

Excellent, thanks Rama :slight_smile:

Coming from doing C# in Unity3d, all this pointer stuff is a bit tricky to get my head around.

I’m happy to have helped out!

:slight_smile:

:slight_smile:

Rama

PS: if your issue is resolved please check mark my answer, or let us know what still needs resolving related to this topic :slight_smile:

There’s a number of problems with your code:

First, TArrays must be defined with a template argument. The syntax is just like Lists in C#. Also the SListView only accepts TArray’s of pointer values. So you should define your arrays as

TArray<TSharedPtr<FString>>

As Rama pointed out SListView also requires a template argument, i.e.

SNew(SListView<TSharedPtr<FString>>)

But most importantly you’re handling your array memory incorrectly. The SListView holds onto a pointer to the array you specify as ListItemSource. That means your array needs to stay around for as long as your SListView is around or the SListView will try to access invalid memory.

You should make your TArray a member/variable of your class instead of a local variable for your function. By making it part of the function it will be cleaned up after the function exits leaving you with unpredictable behavior.

Another thing, you should only ever create a TSharedPtr using this syntax:

TSharedPtr<FString> Item = MakeShareable(new FString())

Creating a TSharedPtr the way you are in your code causes a memory leak/corruption.

Ultimately, you want something like the following:

class SMyGameSlateHUDWidget
{
public:

void SMyGameSlateHUDWidget::Construct(const FArguments& InArgs)
{
    OwnerHUD = InArgs._OwnerHUD;

    Items.Add(MakeShareable(new FString(TEXT("Sup"))));
    Items.Add(MakeShareable(new FString(TEXT("Hi"))));
    Items.Add(MakeShareable(new FString(TEXT("Yo"))));

    ChildSlot
    .VAlign(VAlign_Fill)
    .HAlign(HAlign_Fill)
    [
         SNew(SOverlay)
         +SOverlay::Slot()
         .VAlign(VAlign_Bottom)
         .HAlign(HAlign_Center)
         .Padding(TAttribute(this, &SMyGameSlateHUDWidget::GetPadding))
         [
          SNew(STextBlock)
          .ShadowColorAndOpacity(FLinearColor::Black)
          .ColorAndOpacity(FLinearColor::Red)
          .ShadowOffset(FIntPoint(-1,1))
          .Text(this, &SMyGameSlateHUDWidget::GetSomeString)

         ]
         +SOverlay::Slot()
         .VAlign(VAlign_Center)
         .HAlign(HAlign_Left)
         [

              SNew(SListView<TSharedPtr<FString>>)
              .ItemHeight(24)
              .ListItemsSource(&Items)
              .HeaderRow
              (
                 SNew(SHeaderRow)
                 + SHeaderRow::Column("Name")
                 [
                   SNew(SBorder)
                   .Padding(5)
                   .Content()
                   [
                    SNew(STextBlock)
                    .Text(FString(TEXT("Name")))
                   ]
                 ]
                 + SHeaderRow::Column("Number") .DefaultLabel(FString(TEXT("Number")))
                 + SHeaderRow::Column("TextField") .DefaultLabel(FString(TEXT("Text Field")))
                 + SHeaderRow::Column("TextBlock") .DefaultLabel(FString(TEXT("Text Block")))
                 + SHeaderRow::Column("AddChild") .DefaultLabel(FString(TEXT("Add Child")))
              )


         ]
    ];

}

private:

TArray<TSharedPtr<FString>> Items;

}

Haha, thanks for the help Sarge, unfortunately this question’s actually over a month old, but the way they migrated it from the beta site to this one means you can’t tell, actually, you can’t even tell who asked the question (It was me) which is a bit annoying.

Also, I shiver to look at my code from back then, nasty stuff. To be fair, I shiver at my code now, but only because there’s so much of it. Slate’s line count has a tendency to spiral out of control :stuck_out_tongue: