[UMG]UUserWidgets are invisible when added via c++

I’m fairly new to UE4, having a pretty strong Unity background. Right now, what I’m trying to do is create a progress bar that displays the rewards at various levels. I’m using a pair of Horizontal Boxes in an Overlay, which is in an Overlay with the progress bar.

113719-hierarchy.png

I also have a custom widget that populates the horizontal boxes with the following function:

void UTurretProgressBarWidget::InitializeProgressBar(APlayerController* OwningPlayer)
{
	for (int i = 0; i < MaxLevel; i++)
	{
		UImage* Bar = WidgetTree->ConstructWidget<UImage>(UImage::StaticClass());
		if (Bar)
		{
			UPanelSlot* slot = HorizontalPanelBack->AddChild(Bar);
			UHorizontalBoxSlot* BarSlot = Cast<UHorizontalBoxSlot>(slot);
			Bar->SetColorAndOpacity(FLinearColor(0, 0, 0,1));
			Bar->Brush.ImageSize = FVector2D(4, 32);
			BarSlot->SetHorizontalAlignment (EHorizontalAlignment::HAlign_Left);
			BarSlot->SetVerticalAlignment(EVerticalAlignment::VAlign_Fill);
			FSlateChildSize size = FSlateChildSize(ESlateSizeRule::Fill);
			BarSlot->SetSize(size);
			//BarSlot->Size.Value = 1.0f;
			if (LevelStride > 0)
			{
				if ((i+1) % LevelStride == 0)
				{
					//UTurretLevelSymbolWidget* tempLevelWidget = WidgetTree->ConstructWidget<UTurretLevelSymbolWidget>(MaxLevelWidget->StaticClass());
					UTurretLevelSymbolWidget* tempLevelWidget = CreateWidget<UTurretLevelSymbolWidget>(OwningPlayer);
					if (tempLevelWidget) 
					{
						tempLevelWidget->AddToViewport(1000);
						tempLevelWidget->SetColorAndOpacity(FLinearColor(1, 1, 1, 1));
						UPanelSlot* tempSlot = HorizontalPanelFront->AddChild(tempLevelWidget);
						UHorizontalBoxSlot* levelslot = Cast<UHorizontalBoxSlot>(tempSlot);
						levelslot->SetHorizontalAlignment(EHorizontalAlignment::HAlign_Fill);
						levelslot->SetVerticalAlignment(EVerticalAlignment::VAlign_Fill);
						levelslot->SetSize(size);
						//UE_LOG(LogTemp, Warning, TEXT("CreatedWidget"));
					}
					else
					{
						UE_LOG(LogTemp, Warning, TEXT("Unable to create widget!"));
					}
				}
				else
				{
					USpacer* spacer = WidgetTree->ConstructWidget<USpacer>(USpacer::StaticClass());
					if (spacer) {
						//spacer->SetSize(FVector2D(1.f,1.f));
						UPanelSlot* slot = HorizontalPanelFront->AddChild(spacer);
						UHorizontalBoxSlot* SpacerSlot = Cast<UHorizontalBoxSlot>(slot);
						FSlateChildSize size = FSlateChildSize(ESlateSizeRule::Fill);
						SpacerSlot->SetSize(size);
						SpacerSlot->Size.SizeRule = ESlateSizeRule::Fill;
						//SpacerSlot->Size.Value = 1.0f;
					}
					else
					{
						UE_LOG(LogTemp, Warning, TEXT("Error Spacer is null!"));
					}
				}
			}
		}
	}
}

For whatever reason, the widget that I create via “CreateWidget” is invisible. What am I doing wrong here?

Edit: D’oh! I saw a typo where I was storing a UImage in a USpacer. The widgets are still invisible, though.

Is your “OwningPlayer” reference passing forward the intended player controller?

As far as I can tell. I use a simple “Get Player Controller” call in blueprints and pass that reference into the function when it’s time to call it.

Notably, I can confirm that the widgets are being created based on the child count of the object they’re being parented to.

I’m just curious of whether the widgets are being assigned to the proper player controller (assuming more than one exists) if only one exists, I suggest using the widget reflector in the editor and determining if they are sized correctly and placed appropriately.

Sounds good. I was actually wondering if there was a way to inspect widgets on the fly. The Widget Reflector sounds like exactly what I was looking for.