UE5.7 Windows - UMG text block not fully aligned to center of the screen

Testing outside the editor, I’m using C++ to render a widget on a 1920x1080 viewport

It should render text dead center

In the C++ widget code I center the widget using:

FVector2D size {};

if (GEngine && GEngine->GameViewport)
	GEngine->GameViewport->GetViewportSize(size);
SetPositionInViewport({size.X / 2, size.Y / 2});

I’ve checked the size variable and it is indeed 1920/1080

The widget blueprint itself has a canvas with a single text block inside it

The text block is anchored to the center, position is 0.0/0.0, alignment is 0.5/0.5, size to content is ticked, justification is align text center and pivot is 0.5/0.5

All looks good in the widget editor, the text is center

When I run the game, the text is a tiny bit off from the center of the screen

I know this because if I set the text block pivot to 0.7/0.7, it looks better

Obviously 0.5/0.5 is correct for center

So what is going on? Is some sort of DPI value making it off center?

I think it might be due to DPI. You can try multiplying or dividing your position by the DPI and see if that works.

P.S. To get DPI:
Option 1, using the blueprint function:

#include "Blueprint/WidgetLayoutLibrary.h"
...
float dpiScale = UWidgetLayoutLibrary::GetViewportScale(GetWorld()->GetGameViewport());
...

Option 2, a “more” c++ version:

#include "Engine/UserInterfaceSettings.h"
...
UGameViewportClient* ViewportClient = GetWorld()->GetGameViewport();
FVector2D ViewportSize;
ViewportClient->GetViewportSize(ViewportSize);
float dpiScale = GetDefault<UUserInterfaceSettings>()->GetDPIScaleBasedOnSize(FIntPoint(ViewportSize.X, ViewportSize.Y));
...

I think both options are identical, so it’s up to you which one to use.

Thanks Dasay2605, it wasn’t the DPI scale

In the widget code, this worked for me:

FVector2D size {};
if (GEngine && GEngine->GameViewport)
	GEngine->GameViewport->GetViewportSize(size);
FVector2D widgetSize = GetDesiredSize();
FVector2D pos = (size - widgetSize) * 0.5f;
SetAlignmentInViewport({0.5f, 0.5f});
SetPositionInViewport(pos);