Slate Question

What am I doing wrong in ‘Approach 2’ that I’m not getting the same result as ‘Approach 1’?

Approach 1
//Create a horizontal box and put a STextBlock inside
//works as intended



Menu->AddSlot().AutoHeight().VAlign(VAlign_Fill).HAlign(HAlign_Fill)

    SNew(SHorizontalBox)
    + SHorizontalBox::Slot().VAlign(VAlign_Fill).HAlign(HAlign_Fill)
    
        SNew(STextBlock)
        .Text(LOCTEXT("TextExample", "TextExample").ToString())
        .Font(FSlateFontInfo(FPaths::EngineContentDir() / TEXT("Slate/Fonts/Roboto-Bold.ttf"), 16))
    ]
];


Approach 2
//Create a horizontal box and assign it to ‘ContainerHBox’
//then in a separate call insert a STextBlock within
//does NOT have same result (STextBlock only takes up 1/2 of its parent and aligned right)



TSharedPtr<SHorizontalBox> ContainerHBox;

Menu->AddSlot().AutoHeight().VAlign(VAlign_Fill).HAlign(HAlign_Fill)

    SAssignNew(ContainerHBox,SHorizontalBox)
    + SHorizontalBox::Slot().VAlign(VAlign_Fill).HAlign(HAlign_Fill)
];

ContainerHBox->AddSlot().VAlign(VAlign_Fill).HAlign(HAlign_Fill)

    SNew(STextBlock)
    .Text(LOCTEXT("TextExample", "TextExample").ToString())
    .Font(FSlateFontInfo(FPaths::EngineContentDir() / TEXT("Slate/Fonts/Roboto-Bold.ttf"), 16))
];


I guess my understanding of AddSlot may be wrong, could anyone clarify?

in approach 1 you use + SHorizontalBox::Slot().VAlign(VAlign_Fill).HAlign(HAlign_Fill)

in approach 2 you dont. Try:


ContainerHBox->AddSlot().VAlign(VAlign_Fill).HAlign(HAlign_Fill)

    SNew(STextBlock)
    .Text(LOCTEXT("TextExample", "TextExample").ToString())
    .Font(FSlateFontInfo(FPaths::EngineContentDir() / TEXT("Slate/Fonts/Roboto-Bold.ttf"), 16))
];

szyszek, thanks for the reply but I still get the same behavior. (edited the code now to reflect that)

I feel like calling ‘ContainerHBox->AddSlot()’ adds an extra slot and thats why i’m seeing the TextBlock taking up only 1/2 of its parent and aligned to the right

I experimented with ‘ContainerHBox->Slot()…]’ but nothing appears


SAssignNew(ContainerHBox,SHorizontalBox)
    + SHorizontalBox::Slot().VAlign(VAlign_Fill).HAlign(HAlign_Fill)


Sorry I didnt notice this… you add empty slot to your Container at construction time so after you call ContainerHBox->AddSlot() you have 2 slots in your container, not one. During construction +SHorizontalBox::Slot() works same as AddSlot() later on. So make it like this:



TSharedPtr<SHorizontalBox> ContainerHBox;

Menu->AddSlot().AutoHeight().VAlign(VAlign_Fill).HAlign(HAlign_Fill)

    SAssignNew(ContainerHBox,SHorizontalBox)

];

ContainerHBox->AddSlot().VAlign(VAlign_Fill).HAlign(HAlign_Fill)

    SNew(STextBlock)
    .Text(LOCTEXT("TextExample", "TextExample").ToString())
    .Font(FSlateFontInfo(FPaths::EngineContentDir() / TEXT("Slate/Fonts/Roboto-Bold.ttf"), 16))
];

1 Like

szyszek, ah that did it! Thank you! I misunderstood how SAssignNew works, I mistakenly thought it assigned the next added slot rather than how it works by creating a new one.