# How to draw lines under certain widgets/UI elements?

**URL:** <https://forums.unrealengine.com/t/how-to-draw-lines-under-certain-widgets-ui-elements/2134979>\
**Category:** C++\
**Tags:** unreal-engine, CPP, question\
**Created:** [November 19, 2024, 10:00am UTC](https://forums.unrealengine.com/t/how-to-draw-lines-under-certain-widgets-ui-elements/2134979 "2024-11-19T10:00:26Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [November 19, 2024, 3:41pm UTC](https://forums.unrealengine.com/t/how-to-draw-lines-under-certain-widgets-ui-elements/2134979/2 "2024-11-19T15:41:31Z")

</div>

> [@Draw Line but under the widget (line z order?)](https://forums.unrealengine.com/t/draw-line-but-under-the-widget-line-z-order/696717/8):
>
> Yeah, the solution is simple but kind of silly. Create a different Widget Blueprint with one job, which is drawing lines. Then add this blueprint to your main blueprint, and you can change its order and make it covered by other widgets. However, if you want different lines with different Z-orders, you’ll need to create multiple widgets with lines. If I were to do it now, I would create a generic widget blueprint called something like ‘WBP\_LineDrawer’, where I could draw a single line or arrays…

You can also experiment with passing in a custom LayerId in the NativePaint **strong text** for the MakeLines and super call

.h

```auto
#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "UpgradeWidgetTree.generated.h"

class UCanvasPanel;
/**
 * 
 */
UCLASS()
class YOUR_API UUpgradeWidgetTree : public UUserWidget
{
	GENERATED_BODY()
public:	

	virtual int32 NativePaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const override;
	
	UPROPERTY(meta = (BindWidget))
	UCanvasPanel* InnerCanvas;

	UPROPERTY(meta = (BindWidget))
	UWidget* Slot1;

	UPROPERTY(meta = (BindWidget))
	UWidget* Slot2;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	int32 LineLayerId;
	

};

```

.cpp

```auto

#include "UpgradeWidgetTree.h"
#include "Components/CanvasPanelSlot.h"
#include "Blueprint/WidgetLayoutLibrary.h"
#include "Components/CanvasPanel.h"
#include "Rendering/DrawElementTypes.h"

int32 UUpgradeWidgetTree::NativePaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const
{	
	int32 LayerID = Super::NativePaint(Args, AllottedGeometry, MyCullingRect, OutDrawElements, LayerId, InWidgetStyle, bParentEnabled);
	
	if (InnerCanvas != nullptr && Slot1 != nullptr && Slot2 != nullptr)
	{		
			UCanvasPanelSlot* CanvasSlot0 = UWidgetLayoutLibrary::SlotAsCanvasSlot(Slot1);
			UCanvasPanelSlot* CanvasSlot1 = UWidgetLayoutLibrary::SlotAsCanvasSlot(Slot2);

			FVector2D AntchorPosition = InnerCanvas->GetCachedGeometry().GetLocalSize() * CanvasSlot0->GetAnchors().Minimum;
			FVector2D Position0 = AntchorPosition + CanvasSlot0->GetPosition();
			FVector2D Position1 = AntchorPosition + CanvasSlot1->GetPosition();
												
			FSlateDrawElement::MakeLines(
				OutDrawElements,				
				LineLayerId,				
				InnerCanvas->GetPaintSpaceGeometry().ToPaintGeometry(),
				{ Position0,Position1 },
				ESlateDrawEffect::None,
				FLinearColor::Blue,
				true,
				5.0f
			);					
	}	
	return LayerID;	
}

```

 ![obraz](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/7/5/07588341938101a95b64f739456baaf25c2f8719.png)

Alignment for both boxes is set to 0.5 , 0.5  
Line layer ID = 50

---

_[View the full topic](https://forums.unrealengine.com/t/how-to-draw-lines-under-certain-widgets-ui-elements/2134979)._
