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!

If you use

UButton *tstBtn = ConstructObject(UButton::StaticClass(), this);

Then when you call

directoryFolders->AddChild(tstBtn);

It should add it correctly!