- .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
OnGenerateRowForList(TSharedPtr Item, const TSharedRef& OwnerTable) ; - void OnGetChildrenForTree(TSharedPtr
Item, TArray>& OutChildren) ; -
- private:
- TArray
> Items; -
- TMap
>> ChildrenMap; - };
- .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
("thisA")); - Items.Add(MakeShared
("thisB")); - Items.Add(MakeShared
("thisC")); -
- // 初始化子节点
- ChildrenMap.Add("thisA", TArray
>{ MakeShared("thisAaaa") }); - ChildrenMap.Add("thisB", TArray
>{ MakeShared("thisBaaa"), MakeShared("thisBbbb") }); - ChildrenMap.Add("thisC", TArray
>{ MakeShared("thisCaaa") }); -
- ChildSlot
- [
- SNew(STreeView
>) - .TreeItemsSource(&Items) // Items是一个包含TSharedPtr
的数组 - .OnGenerateRow(this, &SMyTreeView::OnGenerateRowForList)
- .OnGetChildren(this, &SMyTreeView::OnGetChildrenForTree)
- ];
- }
-
- TSharedRef
SMyTreeView::OnGenerateRowForList(TSharedPtr Item, - const TSharedRef
& OwnerTable) - {
- return SNew(STableRow
>, OwnerTable) - [
- SNew(STextBlock)
- .Text(FText::FromString(*Item))
- .Font(FSlateFontInfo(FPaths::EngineContentDir() / TEXT("Slate/Fonts/Roboto-Regular.ttf"), 24)) // 设置字体大小为24
- ];
- }
-
- void SMyTreeView::OnGetChildrenForTree(TSharedPtr
Item, TArray>& OutChildren) - {
- const TArray
>* FoundChildren = ChildrenMap.Find(*Item); - if (FoundChildren)
- {
- OutChildren = *FoundChildren;
- }
- }
-
-
-
-
- END_SLATE_FUNCTION_BUILD_OPTIMIZATION
STableRow的GetExpanderIndentPadding函数控制根节点前的默认图标,要修改需要创建一个继承至STableRow的类,通过重写GetExpanderWidget函数来改变展开和折叠的图标。需要将"ClassIcon.Plus"和"ClassIcon.Minus"替换为自己的样式。
- class SMyTableRow : public STableRow
> - {
- public:
- SLATE_BEGIN_ARGS(SMyTableRow) {}
- SLATE_END_ARGS()
-
- void Construct(const FArguments& InArgs, const TSharedRef
& InOwnerTableView) - {
- STableRow::Construct(
- STableRow::FArguments()
- .Style(FCoreStyle::Get(), "NoBorder")
- .ShowSelection(false),
- InOwnerTableView);
- }
-
- virtual TSharedRef
GenerateWidgetForColumn(const FName& ColumnName) override - {
- return SNew(STextBlock).Text(FText::FromString(*Item));
- }
-
- virtual TSharedRef
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);
- }