C++ Runtime Created Widget not showing on Screen

Hey all,

I’m having an issue where widgets created in C++ on runtime do not show up on my Canvas.

The C++ Code:

		UUserWidget* SlotWidgetItr = CreateWidget<UUserWidget>(GetWorld(), DefaultSlotWidgetClass);


		if (SlotWidgetItr)
		{
			//Add to slot widgets reference array
			SlotWidgets.Add(SlotWidgetItr);
			UCanvasPanelSlot* WidgetOnCanvas = MenuCanvas->AddChildToCanvas(SlotWidgetItr);

			if (WidgetOnCanvas)
			{
				//Set Anchor and alignment to centre
				WidgetOnCanvas->SetAnchors(FAnchors(5.f));
				WidgetOnCanvas->SetAlignment(FVector2D(5.f));
				//Set position
				FVector2D SlotPosition = GetSlotPosition(i, MenuPayload.Num());
				WidgetOnCanvas->SetPosition(SlotPosition);
				UE_LOG(LogTemp, Error, TEXT("Slot %d: %f %f"), i, SlotPosition.X, SlotPosition.Y);

				//Set size to passed size of widget
				WidgetOnCanvas->SetSize(SlotSize);
			}
		}

I’ve run the same code in BP and it works, just not sure if its translating right in C++.

The Widget is a Canvas Panel that’s added to viewport through the Player Controller Class. It runs the above C++ function on its Blueprint Construct.

I’d appreciate it if anyone could tell me what I’m missing.

Thank you!

I’ve had trouble with this in the past and from the answers I got the only solution was that you simply can’t create widgets in c++ as there is a lot of blueprint stuff that goes on behind the scenes that doesn’t happen when you create them through c++. When I was going through this process a few weeks ago I found that the only way that they would ever show up on screen was when I created a blueprint class based on my c++ widget. If you find answers on this they would be extremely helpful to me as well but I’m not sure they answers exist on here.

1 Like

I thought this was the case. I just can’t find what extra code needs to be ran for the widget to be added properly.

Think I’ll just keep it in BP then if I can’t find anything else.

you should also post the name of the function that this code is inside of.