AtstecInc
(AtstecInc)
1
Why don’t work this code?
void UDialogScrollBox::AddNewButtonDialogBlock( TSubclassOf<class UUserWidget> AnimatedButton_BP)
{
if(AnimatedButton_BP)
{
UUserWidget * Button = CreateWidget<UUserWidget>(GetWorld(), AnimatedButton_BP);
UCanvasPanel * ButtonLayout = NewObject<UCanvasPanel>(GetWorld(), TEXT("panel"));
if(ButtonLayout)
{
ButtonLayout->AddChildToCanvas(Button);
UCanvasPanelSlot * ButtonSlot = Cast<UCanvasPanelSlot>(Button->Slot);
if (ButtonSlot)
{
ButtonSlot->SetAutoSize(true);
ButtonSlot->SetAnchors(FAnchors(0.f, 0.f, 1.f, 1.f));
ButtonSlot->SetOffsets(FMargin(0.f, 0.f));
}
this->AddChild(ButtonLayout);
}
}
}
header:
UCLASS()
class AIRAA_API UDialogScrollBox : public UScrollBox
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = " Function")
void AddNewButtonDialogBlock( TSubclassOf<class UUserWidget> AnimatedButton_BP);
#if WITH_EDITOR
virtual const FText GetPaletteCategory();
#endif
};
Define “doesn’t work”. Do you not see the Layout? Or is the button missing?
AtstecInc
(AtstecInc)
3
At startup, the button does not appear
The button visibility is likely false, have you tried the following?
(Note: AddChildToCanvas returns the UCanvasPanelSlot it added the child to, so I simplified your code just a bit)
if (UCanvasPanelSlot * ButtonSlot = ButtonLayout->AddChildToCanvas(Button) )
{
ButtonSlot->SetVisibility(true);
ButtonSlot->SetAutoSize(true);
ButtonSlot->SetAnchors(FAnchors(0.f, 0.f, 1.f, 1.f));
ButtonSlot->SetOffsets(FMargin(0.f, 0.f));
}
AtstecInc
(AtstecInc)
5
But this work well and button appear.
AtstecInc
(AtstecInc)
6
Sorry, but compiler can not resolve symbol SetVisibility
AtstecInc
(AtstecInc)
7
And the button is simply missing
Whoops, should be SetVisibility(EVisibility::Visible); Forgot that takes an enum.
AtstecInc
(AtstecInc)
9
Still not work. Button is missing - not invisible.
AtstecInc
(AtstecInc)
10
Yes, Button and ButtonLayout are not null ptrs.
Have you placed a break point and verified that the Button is being created when you call CreateWidget?
AtstecInc
(AtstecInc)
12
I dont understand, why with last code string button is appear. Do you know why this could be?
if (UCanvasPanelSlot * ButtonSlot = ButtonLayout->AddChildToCanvas(Button))
{
ButtonSlot->SetAutoSize(true);
ButtonSlot->SetAnchors(FAnchors(0.f, 0.f, 1.f, 1.f));
ButtonSlot->SetOffsets(FMargin(0.f, 0.f));
this->AddChild(ButtonLayout);
ButtonLayout->AddChildToCanvas(ButtonLayout->GetChildAt(0));
}
Do you see the scrollbox at least? Or is nothing showing up?
Adding the Button Layout to the Children makes sense (now it’ll be updated and rendered as part of this widget).
However, this line shouldn’t do anything so I’d remove it.
ButtonLayout->AddChildToCanvas(ButtonLayout->GetChildAt(0));
AtstecInc
(AtstecInc)
15
ButtonLayout->AddChildToCanvas(ButtonLayout->GetChildAt(0));
But without this line button not appear. I do not understand this moment.
AtstecInc
(AtstecInc)
16
I found a solution. The problem was in these lines.Without them all work well.
ButtonSlot->SetAutoSize(true);
ButtonSlot->SetAnchors(FAnchors(0.f, 0.f, 1.f, 1.f));
ButtonSlot->SetOffsets(FMargin(0.f, 0.f));
AtstecInc
(AtstecInc)
17
Problem was in property on the image.
If it is TRUE, then this line work well and the button displays all the content that is contained in it
ButtonSlot->SetAutoSize(true);