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);
}
相关推荐
笑鸿的学习笔记1 天前
虚幻引擎5-Unreal Engine笔记之SET节点的输出引脚获取设置后的最新变量值
笔记·ue5·虚幻
Zhichao_971 天前
【UE5.1 C++】VS2022下载安装
ue5
m0_552200822 天前
《UE5_C++多人TPS完整教程》学习笔记37 ——《P38 变量复制(Variable Replication)》
c++·游戏·ue5
曹勖之2 天前
在 UE5 蓝图中配置Actor类型的Asset以作为位置和旋转设置目标
ue5·机器人
曹勖之2 天前
UE 5 和simulink联合仿真,如果先在UE5这一端结束Play,过一段时间以后**Unreal Engine 5** 中会出现显存不足错误
matlab·ue5·机器人
AgilityBaby2 天前
UE5 2D角色PaperZD插件动画状态机学习笔记
笔记·学习·ue5
AgilityBaby2 天前
UE5 创建2D角色帧动画学习笔记
笔记·学习·ue5
ue星空5 天前
UE音频中间件wwise插件
学习·ue5·音视频
AgilityBaby7 天前
UE5打包项目设置Project Settings(打包widows exe安装包)
c++·3d·ue5·游戏引擎·unreal engine
AgilityBaby7 天前
UE5蓝图暴露变量,在游戏运行时修改变量实时变化、看向目标跟随目标Find Look at Rotation、修改玩家自身弹簧臂
笔记·游戏·ue5·游戏引擎·蓝图