# How to deep clone an UMG Widget

**URL:** <https://forums.unrealengine.com/t/how-to-deep-clone-an-umg-widget/376023>\
**Category:** UI\
**Tags:** duplicate, unreal-engine, Widget, CPP, question, UMG\
**Created:** [November 11, 2016, 6:41am UTC](https://forums.unrealengine.com/t/how-to-deep-clone-an-umg-widget/376023 "2016-11-11T06:41:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_0f0b33f3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0f0b33f3](https://forums.unrealengine.com/u/anonymous_user_0f0b33f3)\
**Post date:** [November 11, 2016, 6:41am UTC](https://forums.unrealengine.com/t/how-to-deep-clone-an-umg-widget/376023/1 "2016-11-11T06:41:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_49dabbbf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_49dabbbf](https://forums.unrealengine.com/u/anonymous_user_49dabbbf)\
**Post date:** [May 15, 2017, 9:53pm UTC](https://forums.unrealengine.com/t/how-to-deep-clone-an-umg-widget/376023/2 "2017-05-15T21:53:01Z")

</div>

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;
}

```

---

<div class="post-metadata">

**Author:** ![tommybazar](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tommybazar](https://forums.unrealengine.com/u/tommybazar)\
**Post date:** [September 23, 2021, 3:50am UTC](https://forums.unrealengine.com/t/how-to-deep-clone-an-umg-widget/376023/3 "2021-09-23T03:50:22Z")

</div>

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