Extending the editor but cannot see SListView

Hi, I am trying to extend the editor, I have an editor module with a custom tab. I also have a slate widget class that gets created in the tab, this all works. However I am trying to implement A SListView, with an array of FTexts to learn how to use SListView but it is not going well. I am using the documentation and the SListView.h to figure it out.

Here is the comment from SListView.h

/**
 * A ListView widget observes an array of data items and creates visual representations of these items.
 * ListView relies on the property that holding a reference to a value ensures its existence. In other words,
 * neither SListView<FText> nor SListView<FText*> are valid, while SListView< TSharedPtr<FText> > and
 * SListView< UObject* > are valid.
 *
 * A trivial use case appear below:
 *
 *   Given: TArray< TSharedPtr<FText> > Items;
 *
 *   SNew( SListView< TSharedPtr<FText> > )
 *     .ItemHeight(24)
 *     .ListItemsSource( &Items )
 *     .OnGenerateRow(this, &MyClass::GenerateItemRow)
 *
 * In the example we make all our widgets be 24 screen units tall. The ListView will create widgets based on data items
 * in the Items TArray. When the ListView needs to generate an item, it will do so using the specified OnGenerateRow method.
 *
 * A sample implementation of MyClass::GenerateItemRow has to return a STableRow with optional content:
 *
 * TSharedRef<ITableRow> MyClass::GenerateItemRow(TSharedPtr<FText> Item, const TSharedRef<STableViewBase>& OwnerTable)
 * {
 *     	return SNew(STableRow<TSharedPtr<FText>>, OwnerTable)
 *		[
 *			SNew(STextBlock)
 *			.Text(*Item)
 *		];
 * }
 */

When I do the code suggested by the above comment, I cannot see the list. I know that I can see widget from this class because I have had an STextBlock display text.

Here is my version of the above code:

void MyClass::Construct(const FArguments& Args)
{
    

    TArray< TSharedPtr<FText> > Items;
    Items.Add(MakeShareable(new FText(LOCTEXT("A", "A"))));
    Items.Add(MakeShareable(new FText(LOCTEXT("B", "B"))));
    Items.Add(MakeShareable(new FText(LOCTEXT("C", "C"))));
    

    // Construct the widget
    this->ChildSlot
        [
            SNew(SHorizontalBox)
            + SHorizontalBox::Slot()
            .HAlign(HAlign_Center)
            .VAlign(VAlign_Center)
            [   

                SNew(SListView< TSharedPtr<FText> >)
                .ItemHeight(24)
                 .ListItemsSource(&Items)
                .OnGenerateRow(this, &MyClass::GenerateItemRow)
            ]
    ];
}

TSharedRef<ITableRow> MyClass::GenerateItemRow(TSharedPtr<FText> Item, const TSharedRef<STableViewBase>& OwnerTable)
{
    UE_LOG(LogTemp, Warning, TEXT("GenerateItemRow Text"));

    return SNew(STableRow<TSharedPtr<FText>>, OwnerTable)
        [
            SNew(STextBlock)
            .Text(*Item)
        ];
}

When I do the code from the documentation I get a engine crash here’s the crash report:

Assertion failed: (InCount >= 0) && ((sizeof(InCount) < sizeof(SizeType)) || (InCount <= static_cast<decltype(InCount)>(TNumericLimits<SizeType>::Max()))) [File:F:\Epic Games\UE_5.2\Engine\Source\Runtime\Core\Public\Containers\ArrayView.h] [Line: 209]

UnrealEditor_MyEditor!SListView<TSharedPtr<FText,1> >::ReGenerateItems() [F:\Epic Games\UE_5.2\Engine\Source\Runtime\Slate\Public\Widgets\Views\SListView.h:1357]

Here Is my code:


void MyClass::Construct(const FArguments& Args)
{
    

    TArray< TSharedPtr<FText> > Items;
    Items.Add(MakeShareable(new FText(LOCTEXT("A", "A"))));
    Items.Add(MakeShareable(new FText(LOCTEXT("B", "B"))));
    Items.Add(MakeShareable(new FText(LOCTEXT("C", "C"))));
    

    // Construct the widget
    this->ChildSlot
        [
            SNew(SHorizontalBox)
            + SHorizontalBox::Slot()
            .HAlign(HAlign_Center)
            .VAlign(VAlign_Center)
            [   

                SNew(SListView< TSharedPtr<FText> >)
                .ItemHeight(24)
                 .ListItemsSource(&Items)
                .OnGenerateRow(this, &MyClass::GenerateItemRow)
                .OnContextMenuOpening(this, &MyClass::OnContextMenuOpening)
                .SelectionMode(ESelectionMode::Single)
               .HeaderRow
               (
                   SNew(SHeaderRow)
                   + SHeaderRow::Column("Name")
                   [
                       SNew(SBorder)
                       .Padding(5)
                   .Content()
                   [
                       SNew(STextBlock)
                       .Text(FText::FromString("Name"))
                   ]
                   ]
                    + SHeaderRow::Column("Number").DefaultLabel(FText::FromString("Number"))
                    + SHeaderRow::Column("TextField").DefaultLabel(FText::FromString("Text Field"))
                    + SHeaderRow::Column("TextBlock").DefaultLabel(FText::FromString("Text Block"))
                    + SHeaderRow::Column("AddChild").DefaultLabel(FText::FromString("Add Child"))
                )
            ]
    ];
}

TSharedRef<ITableRow> MyClass::GenerateItemRow(TSharedPtr<FText> Item, const TSharedRef<STableViewBase>& OwnerTable)
{


    return SNew(STableRow<TSharedPtr<FText>>, OwnerTable)
        [
            SNew(STextBlock)
            .Text(*Item)
        ];
}

TSharedPtr<SWidget> MyClass::OnContextMenuOpening()
{
    TSharedPtr<SWidget> ContextMenuWidget;
    
    FMenuBuilder MenuBuilder(true, nullptr);

    MenuBuilder.BeginSection("ContextMenuSection", FText::FromString("Context Menu"));
    {
        MenuBuilder.AddMenuEntry(
            FText::FromString("Option 1"),
            FText::FromString("Description for Option 1"),
            FSlateIcon(),
            FUIAction(FExecuteAction::CreateRaw(this, &MyClass::MenuOption))
        );
        
        
    }
    MenuBuilder.EndSection();

    ContextMenuWidget = MenuBuilder.MakeWidget();

    return ContextMenuWidget;
}

void MyClass::MenuOption()
{
}

Anyone know what is going wrong?

1 Like

Just found the solution, but I will leave this here if anyone has the same issue. I fixed it by moving the TArray< TSharedPtr > Items; into the header file

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.