UE5 Slate中的StreeView的基础创建方法

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

STableRowGetExpanderIndentPadding函数控制根节点前的默认图标,要修改需要创建一个继承至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);
}
相关推荐
邪修king2 天前
UE5 C++ 游戏性能优化:大一也能学会的实战级优化指南
c++·游戏·ue5
HAPPY酷3 天前
[UE5 避坑指南] 为什么打包后 UI 消失了?Launch Game 与强制加载
java·ui·ue5
晴夏。5 天前
unlua实现原理
游戏·ue5·ue4·lua·ue·unlua
晴夏。5 天前
UE Spawn出来的Actor的生命周期和管理方法
游戏·ue5·ue4·ue
晴夏。6 天前
UE垃圾回收的全方面讲解(通俗易懂)【底层实现、触发方式、引用保持、优化、工具】
ue5·游戏引擎·ue·垃圾回收
邪修king6 天前
UE5:C++ 实现 游戏逻辑 ↔ UI 双向联动
c++·游戏·ue5
HAPPY酷7 天前
从Public到Private:UE5 C++类创建路径差异全解析
java·c++·ue5
1204157137 肖哥10 天前
UE5.7 Procedural Vegetation分析
ue5
半天法师10 天前
Bug 记录:UE 结构体转 JSON 时 Key 字段大小写异常 (Editor 与打包后表现不一致)
ai·ue5·json·bug
邪修king10 天前
UE5 零基础入门第四弹:UMG UI 系统入门,从静态界面到逻辑联动
c++·ui·ue5