Child not showing in hierarchy

Hi,

I’ve been struggling with the follow issue for a while:

Im trying to add an object of type “UPanelWidget” to an “UCanvasPanel”
The classtype of UPanelWidget, so for instance a UHorizontalBox is being defined in the editor.

Code CPP:

void URRInputList::OnWidgetRebuilt(){
	Super::OnWidgetRebuilt();

	if (PanelClass != nullptr) {
		ItemsPanel = ConstructObject<UPanelWidget>(PanelClass);
		if (ItemsPanel != NULL) {
			UPanelSlot* Slot = AddChild(ItemsPanel);
		}
		else {
			PrintError(TEXT("ItemsPanel did not get created properly."))
		}
	}
}

Code Header:

class UPanelWidget;

UCLASS(BluePrintable)

class ROADRAGE_API URRInputList : public UCanvasPanel {
	GENERATED_BODY()
public:
	UPROPERTY(EditAnywhere)
	TSubclassOf<UPanelWidget> PanelClass;

	UPROPERTY()
	UPanelWidget* ItemsPanel;

	virtual void OnWidgetRebuilt() override;
};

When debugging both the ItemsPanel and Slot are valid but in editor I see the following (without the mad drawing):

Hello MaasB,

I don’t have much experience setting up UMG related classes in code, but from looking at what you provided, the “ItemsPanel” UPROPERTY is not a “Child” of any class, it is a property meaning that it will only be displayed in the Details panel.

An example of a “Child” would be the relation between your RRInputList class and the Canvas Panel. If you wish for the ItemsPanel to display under RRInputList, you’ll need to have it be a separate class definition that inherits from RRInputList.

I hope this helps!

Thanks for your response , but the ItemsPanel is being added as a child by the RRInputlist that is already present in the hierarchy (please see screenshot), the UPROPERTY is just there for me to verify the item has been created properly. When calling AddChild I would assume the Object that is being added would become a child of the caller, in this case, RRInputList would have a child ItemsPanel, which in this case is a Horizontal Box (class defined by the UPROPERTY PanelClass).

I hope this clarifies my issue!

Thanks!

Hello MaasB,

The reason this isn’t working is because the difference between templates and instances. The designer itself is working off the template, while your logic is treating it like an instance. This means that while AddChild is being called, it won’t affect the designer. If you wish to work off the template, I would be best to place your logic for AddChild inside of OnCreationFromPalette(). This can be overridden and used in your custom class.

Hope this helps!

Thank you so much!

OnCreationFromPalette() did the trick :slight_smile:

For people to keep in mind, this will only work when the PanelClass is defined before spawning the widget, so I solved this by creating a blueprint that has RRInputList as a parent class and defined the class inside the blueprint.
So now when I drag the inputlist blueprint inside another widget, the panelclass is known when OnCreationFromPalette() is called and the itemspanel is visible in the hierarchy, hooray!

Thanks again :slight_smile: