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


    1. .h
    2. // Fill out your copyright notice in the Description page of Project Settings.
    3. #pragma once
    4. #include "CoreMinimal.h"
    5. #include "Widgets/SCompoundWidget.h"
    6. /**
    7. *
    8. */
    9. class MYALONESLATE_API SMyTreeView : public SCompoundWidget
    10. {
    11. public:
    12. SLATE_BEGIN_ARGS(SMyTreeView)
    13. {
    14. }
    15. SLATE_ARGUMENT(FText,buttontext)
    16. SLATE_END_ARGS()
    17. /** Constructs this widget with InArgs */
    18. void Construct(const FArguments& InArgs);
    19. TSharedRef OnGenerateRowForList(TSharedPtr Item, const TSharedRef& OwnerTable);
    20. void OnGetChildrenForTree(TSharedPtr Item, TArray>& OutChildren);
    21. private:
    22. TArray> Items;
    23. TMap>> ChildrenMap;
    24. };
    1. .cpp
    2. // Fill out your copyright notice in the Description page of Project Settings.
    3. #include "MyTreeView.h"
    4. #include "SlateOptMacros.h"
    5. BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
    6. void SMyTreeView::Construct(const FArguments& InArgs)
    7. {
    8. // 初始化根节点
    9. Items.Add(MakeShared("thisA"));
    10. Items.Add(MakeShared("thisB"));
    11. Items.Add(MakeShared("thisC"));
    12. // 初始化子节点
    13. ChildrenMap.Add("thisA", TArray>{ MakeShared("thisAaaa") });
    14. ChildrenMap.Add("thisB", TArray>{ MakeShared("thisBaaa"), MakeShared("thisBbbb") });
    15. ChildrenMap.Add("thisC", TArray>{ MakeShared("thisCaaa") });
    16. ChildSlot
    17. [
    18. SNew(STreeView>)
    19. .TreeItemsSource(&Items) // Items是一个包含TSharedPtr的数组
    20. .OnGenerateRow(this, &SMyTreeView::OnGenerateRowForList)
    21. .OnGetChildren(this, &SMyTreeView::OnGetChildrenForTree)
    22. ];
    23. }
    24. TSharedRef SMyTreeView::OnGenerateRowForList(TSharedPtr Item,
    25. const TSharedRef& OwnerTable)
    26. {
    27. return SNew(STableRow>, OwnerTable)
    28. [
    29. SNew(STextBlock)
    30. .Text(FText::FromString(*Item))
    31. .Font(FSlateFontInfo(FPaths::EngineContentDir() / TEXT("Slate/Fonts/Roboto-Regular.ttf"), 24)) // 设置字体大小为24
    32. ];
    33. }
    34. void SMyTreeView::OnGetChildrenForTree(TSharedPtr Item, TArray>& OutChildren)
    35. {
    36. const TArray>* FoundChildren = ChildrenMap.Find(*Item);
    37. if (FoundChildren)
    38. {
    39. OutChildren = *FoundChildren;
    40. }
    41. }
    42. END_SLATE_FUNCTION_BUILD_OPTIMIZATION

    STableRowGetExpanderIndentPadding函数控制根节点前的默认图标,要修改需要创建一个继承至STableRow的类,通过重写GetExpanderWidget函数来改变展开和折叠的图标。需要将"ClassIcon.Plus""ClassIcon.Minus"替换为自己的样式。

    1. class SMyTableRow : public STableRow>
    2. {
    3. public:
    4. SLATE_BEGIN_ARGS(SMyTableRow) {}
    5. SLATE_END_ARGS()
    6. void Construct(const FArguments& InArgs, const TSharedRef& InOwnerTableView)
    7. {
    8. STableRow::Construct(
    9. STableRow::FArguments()
    10. .Style(FCoreStyle::Get(), "NoBorder")
    11. .ShowSelection(false),
    12. InOwnerTableView);
    13. }
    14. virtual TSharedRef GenerateWidgetForColumn(const FName& ColumnName) override
    15. {
    16. return SNew(STextBlock).Text(FText::FromString(*Item));
    17. }
    18. virtual TSharedRef GetExpanderWidget() override
    19. {
    20. return SNew(SImage)
    21. .Image(this, &SMyTableRow::GetExpanderImage);
    22. }
    23. const FSlateBrush* GetExpanderImage() const
    24. {
    25. static FSlateBrush PlusImage = FSlateIconFinder::FindIconBrushForClass("ClassIcon.Plus");
    26. static FSlateBrush MinusImage = FSlateIconFinder::FindIconBrushForClass("ClassIcon.Minus");
    27. return IsItemExpanded() ? &MinusImage : &PlusImage;
    28. }
    29. };

    然后在OnGenerateRowForList函数中使用SMyTableRow来替换STableRow

    1. TSharedRef<ITableRow> SMyTreeView::OnGenerateRowForList(TSharedPtr<FString> Item, const TSharedRef<STableViewBase>& OwnerTable)
    2. {
    3. return SNew(SMyTableRow, OwnerTable)
    4. .Item(Item);
    5. }

  • 相关阅读:
    Swift SwiftUI CoreData 过滤数据 2
    【从零开始游戏开发】静态资源优化 | 全面总结 |建议收藏
    新媒体背景下大学生的志愿服务参与研究(lunwen+任务书)
    推荐系统_所有评价模块
    spring boot 引入hive
    【MySQL】5.触发器
    CFD基本概念
    基于ssm的图书馆绘本馆的设计与开发-计算机毕业设计源码
    UnitTesting 单元测试
    Vue中的计算属性和侦听器有什么区别?
  • 原文地址:https://blog.csdn.net/weixin_42318094/article/details/136243107