How to deep clone an UMG Widget

Hi all~

Here is the thing.

I have a grid in my UMG…And I need to copy it serveral times due to the server data.So, I have to clone this

widget…

After searching message in the ue4 forum…There is two api but none of them can do whati want…

The first one is NewObject,It seems that this api will only clone the parent node.

The second one is DuplicateObject.It will clone all the widget.But only the parent pointer is different.So if i click the

child which is a button.All button duplicated will be pressed.So i guess only the parent node is deep clone,all the

children nodes are shallow clone…

So,these two api cannot help me… Which api should i use???

Thanks.

Can’t find. This is my manual solution:

UWidget* UMyModalDialog::DeepDuplicateWidget(UWidget *pUWidget)
{
	UWidget *pNewWidget = DuplicateObject<UWidget>(pUWidget, this);
	UPanelWidget *pNewUPanelWidget = Cast<UPanelWidget>(pNewWidget);
	if (pNewUPanelWidget)
	{
		const TArray<UPanelSlot*>& slots = pNewUPanelWidget->GetSlots();
		for (int32 iSlotNum = 0; iSlotNum < slots.Num(); ++iSlotNum)
		{
			slots[iSlotNum]->Content = nullptr;
		}
		pNewUPanelWidget->ClearChildren();
		UPanelWidget *pUPanelWidget = Cast<UPanelWidget>(pUWidget);
		for (int ii = 0; ii < pUPanelWidget->GetChildrenCount(); ++ii)
		{
			UWidget *pChildUWidget = pUPanelWidget->GetChildAt(ii);
			UWidget *pNewChildWidget = DeepDuplicateWidget(pChildUWidget);
			UPanelSlot *pUPanelSlot = pNewUPanelWidget->AddChild(pNewChildWidget);
			UHorizontalBoxSlot *pNewUHorizontalBoxSlot = Cast<UHorizontalBoxSlot>(pUPanelSlot);
			if (pNewUHorizontalBoxSlot)
			{
				UHorizontalBoxSlot *pUHorizontalBoxSlot = Cast<UHorizontalBoxSlot>(pChildUWidget->Slot);
				pNewUHorizontalBoxSlot->SetHorizontalAlignment(pUHorizontalBoxSlot->HorizontalAlignment);
				pNewUHorizontalBoxSlot->SetVerticalAlignment(pUHorizontalBoxSlot->VerticalAlignment);
			}
			USizeBoxSlot *pNewUSizeBoxSlot = Cast<USizeBoxSlot>(pUPanelSlot);
			if (pNewUSizeBoxSlot)
			{
				USizeBoxSlot *pUSizeBoxSlot = Cast<USizeBoxSlot>(pChildUWidget->Slot);
				pNewUSizeBoxSlot->SetHorizontalAlignment(pUSizeBoxSlot->HorizontalAlignment);
				pNewUSizeBoxSlot->SetVerticalAlignment(pUSizeBoxSlot->VerticalAlignment);
			}
		}
	}

	return pNewWidget;
}
1 Like

There is an overload of NewObject that takes a UObject* Template parameter. I believe this will copy it properly.