[UMG] Not display added in c++ CanvasPanel

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?

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));
             }

But this work well and button appear.

Sorry, but compiler can not resolve symbol SetVisibility

And the button is simply missing

Whoops, should be SetVisibility(EVisibility::Visible); Forgot that takes an enum.

Still not work. Button is missing - not invisible.

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?

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));

ButtonLayout->AddChildToCanvas(ButtonLayout->GetChildAt(0));
But without this line button not appear. I do not understand this moment.

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));

Problem was in property on the image.

130364-снимок.jpg

If it is TRUE, then this line work well and the button displays all the content that is contained in it

ButtonSlot->SetAutoSize(true);