cpp
.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Widgets/SCompoundWidget.h"
/**
*
*/
class MYALONESLATE_API SMyTreeView : public SCompoundWidget
{
public:
SLATE_BEGIN_ARGS(SMyTreeView)
{
}
SLATE_ARGUMENT(FText,buttontext)
SLATE_END_ARGS()
/** Constructs this widget with InArgs */
void Construct(const FArguments& InArgs);
TSharedRef<ITableRow> OnGenerateRowForList(TSharedPtr<FString> Item, const TSharedRef<STableViewBase>& OwnerTable);
void OnGetChildrenForTree(TSharedPtr<FString> Item, TArray<TSharedPtr<FString>>& OutChildren);
private:
TArray<TSharedPtr<FString>> Items;
TMap<FString, TArray<TSharedPtr<FString>>> ChildrenMap;
};
cpp
.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyTreeView.h"
#include "SlateOptMacros.h"
BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
void SMyTreeView::Construct(const FArguments& InArgs)
{
// 初始化根节点
Items.Add(MakeShared<FString>("thisA"));
Items.Add(MakeShared<FString>("thisB"));
Items.Add(MakeShared<FString>("thisC"));
// 初始化子节点
ChildrenMap.Add("thisA", TArray<TSharedPtr<FString>>{ MakeShared<FString>("thisAaaa") });
ChildrenMap.Add("thisB", TArray<TSharedPtr<FString>>{ MakeShared<FString>("thisBaaa"), MakeShared<FString>("thisBbbb") });
ChildrenMap.Add("thisC", TArray<TSharedPtr<FString>>{ MakeShared<FString>("thisCaaa") });
ChildSlot
[
SNew(STreeView<TSharedPtr<FString>>)
.TreeItemsSource(&Items) // Items是一个包含TSharedPtr<FString>的数组
.OnGenerateRow(this, &SMyTreeView::OnGenerateRowForList)
.OnGetChildren(this, &SMyTreeView::OnGetChildrenForTree)
];
}
TSharedRef<ITableRow> SMyTreeView::OnGenerateRowForList(TSharedPtr<FString> Item,
const TSharedRef<STableViewBase>& OwnerTable)
{
return SNew(STableRow<TSharedPtr<FString>>, OwnerTable)
[
SNew(STextBlock)
.Text(FText::FromString(*Item))
.Font(FSlateFontInfo(FPaths::EngineContentDir() / TEXT("Slate/Fonts/Roboto-Regular.ttf"), 24)) // 设置字体大小为24
];
}
void SMyTreeView::OnGetChildrenForTree(TSharedPtr<FString> Item, TArray<TSharedPtr<FString>>& OutChildren)
{
const TArray<TSharedPtr<FString>>* FoundChildren = ChildrenMap.Find(*Item);
if (FoundChildren)
{
OutChildren = *FoundChildren;
}
}
END_SLATE_FUNCTION_BUILD_OPTIMIZATION
STableRow
的GetExpanderIndentPadding
函数控制根节点前的默认图标,要修改需要创建一个继承至STableRow的类,通过重写GetExpanderWidget
函数来改变展开和折叠的图标。需要将"ClassIcon.Plus"
和"ClassIcon.Minus"
替换为自己的样式。
cpp
class SMyTableRow : public STableRow<TSharedPtr<FString>>
{
public:
SLATE_BEGIN_ARGS(SMyTableRow) {}
SLATE_END_ARGS()
void Construct(const FArguments& InArgs, const TSharedRef<STableViewBase>& InOwnerTableView)
{
STableRow::Construct(
STableRow::FArguments()
.Style(FCoreStyle::Get(), "NoBorder")
.ShowSelection(false),
InOwnerTableView);
}
virtual TSharedRef<SWidget> GenerateWidgetForColumn(const FName& ColumnName) override
{
return SNew(STextBlock).Text(FText::FromString(*Item));
}
virtual TSharedRef<SWidget> GetExpanderWidget() override
{
return SNew(SImage)
.Image(this, &SMyTableRow::GetExpanderImage);
}
const FSlateBrush* GetExpanderImage() const
{
static FSlateBrush PlusImage = FSlateIconFinder::FindIconBrushForClass("ClassIcon.Plus");
static FSlateBrush MinusImage = FSlateIconFinder::FindIconBrushForClass("ClassIcon.Minus");
return IsItemExpanded() ? &MinusImage : &PlusImage;
}
};
然后在OnGenerateRowForList
函数中使用SMyTableRow
来替换STableRow
:
TSharedRef<ITableRow> SMyTreeView::OnGenerateRowForList(TSharedPtr<FString> Item, const TSharedRef<STableViewBase>& OwnerTable)
{
return SNew(SMyTableRow, OwnerTable)
.Item(Item);
}