I’m trying to populate a UScrollBox with Images using c++. The images are stored in a DataTable. I get a Nullptr when I try to add UColorButton to my scrollbox. Using Debug Tool Widget Reflector I can see that that children are added with Y size 0, but still null when I hit play.
cpp for ColorButton, I get NullPtr in here
#include "UserInterface/Construction/ColorButton.h"
#include "Components/Image.h"
void UColorButton::NativeConstruct()
{
Super::NativeConstruct();
bIsFocusable = true;
}
UColorButton::UColorButton(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
}
void UColorButton::SetTexture(UTexture2D* NewTexture)
{
if (IconImage)
{
UE_LOG(LogTemp, Warning, TEXT("IconImage is not a nullptr"));
IconImage->SetBrushFromTexture(NewTexture);
}
else {
UE_LOG(LogTemp, Warning, TEXT("IconImage is a nullptr")); //This happens
}
}
header
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "ColorButton.generated.h"
class UImage;
UCLASS()
class MyGame_API UColorButton : public UUserWidget
{
GENERATED_BODY()
public:
UColorButton(const FObjectInitializer& ObjectInitializer);
UFUNCTION(BlueprintCallable, Category = "Texture Widget")
void SetTexture(UTexture2D* NewTexture);
UPROPERTY(meta = (BindWidget))
UImage* IconImage;
protected:
virtual void NativeConstruct() override;
};
Then I create the widget inside my UConstructionWidget. NullPtr when I call “SetTexture”
void UConstructionUserWidget::NativeConstruct()
{
Super::NativeConstruct();
for (auto& it : ColorIconDataTable->GetRowMap()) {
UColorButton* ColorImage = CreateWidget<UColorButton>(this, UColorButton::StaticClass());
FWidgetDataStruct* Row = ColorIconDataTable->FindRow<FWidgetDataStruct>(it.Key, TEXT(""));
if (Row){
//ScrollBox
UTexture2D* TextureFromDataTable = Row->Icon.IconImage;
ColorImage->SetTexture(TextureFromDataTable);
ColorWheelScroll->AddChild(ColorImage);
}
}
header of UConstructionWidget
UPROPERTY(VisibleAnywhere, meta = (BindWidget), Category = "Construction")
UScrollBox* ColorWheelScroll;
UPROPERTY(EditAnywhere, BlueprintReadOnly)
UDataTable* ColorIconDataTable;
I have created Blueprints of both of them. And used the meta binding. What am I missing?