在UMG中继承了UserObjectListEntry,代码通过Cast<IUserObjectListEntry>(&Widget)转换失败,查看源码后发现还有个UUserObjectListEntry。
IInterface是在C++中使用,而UInterface是封装了Interface,因为蓝图对接口的支持不太行具体原因没有深入研究。
实现原理:在OnEntryWidgetGenerated后手动展开当前节点
// Fill out your copyright notice in the Description page of Project Settings.
#include "Widget/TreeViewEx.h"
UTreeViewEx::UTreeViewEx(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
OnEntryWidgetGenerated().AddUObject(this, &UTreeViewEx::InitAllChildren);
}
void UTreeViewEx::InitAllChildren(UUserWidget& Widget)
{
TSubclassOf<UUserObjectListEntry> UserObjectListEntry = UUserObjectListEntry::StaticClass();
//直接Cast会失败,因为蓝图使用的接口是通过UInterface实现的
if (IUserObjectListEntry* Entry = Cast<IUserObjectListEntry>(&Widget))
{
SetItemExpansion(Entry->GetListItem(), true);
}
else if (Widget.GetClass()->ImplementsInterface(UserObjectListEntry))
{
TScriptInterface<IUserObjectListEntry> MyInterface;
MyInterface.SetObject(&Widget);
if (MyInterface.GetObject())
{
auto ListItem = UUserObjectListEntryLibrary::GetListItemObject(MyInterface);
SetItemExpansion(ListItem, true);
}
}
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/TreeView.h"
#include "TreeViewEx.generated.h"
/**
*
*/
UCLASS()
class XXX_API UTreeViewEx : public UTreeView
{
GENERATED_BODY()
public:
UTreeViewEx(const FObjectInitializer& ObjectInitializer);
private:
void InitAllChildren(UUserWidget& Widget);
};
|