I’m not certain this is a bug or not. Maybe the problem I’m seeing is due to something I’m doing. Additionally, I have another post that I’m pasting here but with added observations. From my other post:
When my Menu first starts my widgets are aligned as expected. However, when I leave and return to the Menu the text are concatenated. If I reload the Menu level, returning from a game for example, it works fine for the first time. Going into and exiting from sub-menus appear to be giving me a problem.
On my Menu I have a title bar, and a scrollbox that I need to recreate when entering the Menu. The widgets consist of UScrollbox, UHorizontalBox, UCanvasPanel, UTextBlock, UImage, etc.
On a different screen, where the controls are added to the scrollbox using a top level panel (UCanvasPanel), that is, one panel per row, it works as expected. In fact, my Menu originally employed the same technique and worked perfectly except some text was being truncated.
Since working with UE4 and the widgets, I have moved to starting with horizontal boxes (UHorizontalBox) as the top widget for the scrollbox row, then nesting additional horizontal boxes for better control and positioning. And it appears to work well, with the exception of the problem that I’m having.
During construction I use myWidget->ClearChildren(), then build up the control. I’ve tried adding the myWidget->ForceLayoutPrepass() too but that didn’t help. Not sure that it’s a deleting widget issue.
I’ve tried many different gyrations to correct the problem but no luck thus far. Has anyone else encountered a similar problem?
ADDED OBSERVATIONS
I noticed that the Canvas Panel Slots are empty and the MyWidget is invalid the first time through. When trying to reconstruct the widgets a second time the alignment is ignored.
UHorizontalBox* line = NewObject<UHorizontalBox>(this, UHorizontalBox::StaticClass());
titleBar->AddChildToCanvas(line);
FVector2D size = Cast<UCanvasPanelSlot>(titleBar->Slot)->GetSize();
Cast<UCanvasPanelSlot>(line->Slot)->SetSize(size);
Cast<UCanvasPanelSlot>(line->Slot)->SetZOrder(0);
// left spacer box
UHorizontalBox* leftSpacerBox = CreateHorizontalBox(line, ESlateSizeRule::Fill, 0.1f);
for (int32 x = 0; x < titleLine.Num(); x++)
{
UHorizontalBox* box = CreateHorizontalBox(line, ESlateSizeRule::Fill, titleLine[x].slotSize, titleLine[x].hAlign);
UTextBlock* text = CreateTextBlock(box, titleLine[x].text, titleLine[x].justify, FLinearColor(1.0f, 1.0f, 0.0f, 1.0f), 28);
}
// right spacer box
UHorizontalBox* rightSpacerBox = CreateHorizontalBox(line, ESlateSizeRule::Fill, 0.1f);
UHorizontalBox* UMyMenuScreenWidget::CreateHorizontalBox(UHorizontalBox* line,
ESlateSizeRule::Type rule,
float value,
EHorizontalAlignment hAlign,
EVerticalAlignment vAlign)
{
UHorizontalBox* box = NewObject<UHorizontalBox>(this, UHorizontalBox::StaticClass());
line->AddChild(box);
UHorizontalBoxSlot* slot = Cast<UHorizontalBoxSlot>(box->Slot);
slot->Size.SizeRule = rule;
slot->Size.Value = value;
slot->HorizontalAlignment = hAlign;
slot->VerticalAlignment = vAlign;
return box;
}
UTextBlock* UMyMenuWidget::CreateTextBlock(UHorizontalBox* line,
FText text,
ETextJustify::Type justify,
FLinearColor color,
int32 fontSize,
FMargin margin,
ESlateVisibility visibility)
{
UTextBlock* textBlock = NewObject<UTextBlock>(this, UTextBlock::StaticClass());
line->AddChild(textBlock);
textBlock->SetVisibility(visibility);
textBlock->SetText(text);
textBlock->Font.Size = fontSize;
textBlock->SetColorAndOpacity(FSlateColor(color));
textBlock->Margin = margin;
textBlock->SetJustification(justify);
return textBlock;
}