How to create widget in C++?

Hi, i want to create widget in C++ like attached picture. My codes don’t work.(nothing shows up in game) Please tell me why it doesn’t work?

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "Components/TextBlock.h"
#include "Components/CanvasPanel.h"
#include "DeathWidget.generated.h"

Header file
class TEST_API UDeathWidget : public UUserWidget

	virtual void NativePreConstruct() override;

	UTextBlock* DisplayTextBlock;


C++ file

#include "DeathWidget.h"
#include "Components/CanvasPanel.h"
#include "Blueprint/WidgetTree.h"
#include "Components/Button.h"

void UDeathWidget::NativePreConstruct()

	UWidget* rootWidget = GetRootWidget();
	UCanvasPanel* Root = static_cast<UCanvasPanel*>(rootWidget);

	DisplayTextBlock = WidgetTree->ConstructWidget<UTextBlock>(UTextBlock::StaticClass(), FName("Death_Widget"));
	DisplayTextBlock->SetText(FText::FromString("Press 7 to Re-spawn"));
	DisplayTextBlock->Font.TypefaceFontName = FName(TEXT("Regular"));
	DisplayTextBlock->Font.Size = 34;


Make pointer for your widget and put meta = (BindWidget) specifier in it’s UPROPERTY. You don’t need to construct anything widget code will do everything for you, all you need to do is just set properties you want.

Since you in to C++ you might also look in to creating your own UWidget with slate as UMG is just Blueprint wrapper for slate and Slate is a lot more easier and fun to use in C++

It as easy as declaring Slate widget in RebuildWidget() and make property sync SynchronizeProperties() (without it in widget blueprint editor widget does not update)

i think this video will be helpfull. How to Spawn/Create Widget in Unreal Engine C++ - YouTube