Add Widgets to UScrollBox through C++

Hello, I’ve been looking around and only seen this implemented in Blueprint. I simply want to add some buttons to a ScrollBox in C++.

I tried simply adding a child button to see if that would work, but the game would just crash on load. My UScrollBox is the variable ‘directoryFolders’.


     void UDirectoryWidget::updateDirectoryFolders(){
         UButton testButton;
         directoryFolders->AddChild(&testButton);
     }

I also tried casting ‘testButton’ to (UWidget*) but that did not work either. Any insight would be great!

I found this:

Thanks Ariegos! When I compiled it worked as expected, though it did say that ‘ConstructObject’ was depreciated. So I went ahead and used ‘NewObject’ instead and it seemed to work exactly the same without the warning.

This is what I did:


UButton* testButton = NewObject<UButton>(this, UButton::StaticClass());	
directoryFolders->AddChild(testButton);

Where the directoryFolders variable is a pointer to my ScrollBox.

Best

1 Like