Hello everyone, i have a question about slate. There are two widgets: Uniform Grid Panels, Wrap Boxes. i know that
through +SWrapBox::Slot() i can add items inside of this widget, but i need to add them through loop of array, but i have no clue how to do it. So if anybody knows how to do it, please help
You can loop over your array and then call AddSlot() on the SWrapBox instance. This will do the same thing as +SWrapBox::Slot() would do when using the Slate declarative syntax.
TSharedRef<SWrapBox> WrapBox = SNew(SWrapBox);
for(const auto& Item : YourArray)
{
WrapBox->AddSlot()
[
/** Add your widget here */
];
}
hey, thanks, but did i make mistake? because this one doesnt work, it shows nothing
TArray strArr;
strArr.Add("hello");
strArr.Add("Hello 1323");
strArr.Add("hello dwadw23");
TSharedRef WrapBox = SNew(SWrapBox);
for (const auto& Item : strArr)
{
WrapBox->AddSlot()
[
SNew(STextBlock)
.Text(FText::FromString(Item))
];
}
ChildSlot
.VAlign(VAlign_Fill)
.HAlign(HAlign_Fill)
[
SNew(SVerticalBox)
+ SVerticalBox::Slot()
.AutoHeight()
[
SNew(SBox)
.WidthOverride(600)
.HeightOverride(300)
[
SAssignNew(WrapBox, SWrapBox)
.PreferredWidth(100.f)
]
]
];
Close…
In the loop, Item is your FString in the array.
You already have the WrapBox, so your SAssignNew is replacing that instance.
The following should work, although I haven’t tested it.
TArray<FString> strArr;
strArr.Add("hello");
strArr.Add("Hello 1323");
strArr.Add("hello dwadw23");
TSharedRef<SWrapBox> WrapBox = SNew(SWrapBox)
.PreferredWidth(100.f);
for (const FString& Item : strArr)
{
WrapBox->AddSlot()
[
SNew(STextBlock)
.Text(FText::FromString(Item))
];
}
ChildSlot
.VAlign(VAlign_Fill)
.HAlign(HAlign_Fill)
[
SNew(SVerticalBox)
+ SVerticalBox::Slot()
.AutoHeight()
[
SNew(SBox)
.WidthOverride(600)
.HeightOverride(300)
[
WrapBox
]
]
];
perfect, it works, thanks a lot ![]()
Thank you for asking, I have the exact same question.