how to show a button made in c++ in the Designer Editor hierarchy

hi , I made canvas panel and a button in c++ but they are not being shown in designer editor hierarchy for further editing and placement. Is there some kind of UPROPERTY() or something, or is it a completely different concept. Any help is appreciated.

Thank you.

Hey there, if they derive from UUserWidget then it should show up, try to restart the engine.

thanks for the reply, please take a look again I added an image to clarify things

What type of changes did you do on the button?

here is code for Widget

.h file -
#include “CoreMinimal.h”
#include “Blueprint/UserWidget.h”
#include “UMG.h”
#include “RestartWidget.generated.h”

UCLASS()
class TEST_API URestartWidget : public UUserWidget
{
GENERATED_BODY()

protected:

virtual void NativeConstruct() override;
virtual TSharedRef RebuildWidget();

UPROPERTY(EditAnywhere, BlueprintReadWrite)
class UButton * RestartButton;

UPROPERTY(EditAnywhere, BlueprintReadWrite)
class UPanelWidget *RootWidget;

UFUNCTION()
void OnRestartClicked();
};

.cpp file -

#include “RestartWidget.h”
#include “Runtime/UMG/Public/UMG.h”

void URestartWidget::NativeConstruct()
{
Super::NativeConstruct();
}
TSharedRef URestartWidget::RebuildWidget()
{
if (WidgetTree)
{
RootWidget = Cast(GetRootWidget());
if (RootWidget == nullptr)
{
RootWidget = WidgetTree->ConstructWidget(UCanvasPanel::StaticClass(), FName(“RootWidget”));

UCanvasPanelSlot * RootSlot = Cast(RootWidget->Slot);
if (RootSlot)
{
RootSlot->SetAnchors(FAnchors(0.f, 0.f, 1.f, 1.f));
RootSlot->SetOffsets(FMargin(0.f, 0.f));
}

			WidgetTree->RootWidget = RootWidget;
		}
	}

	TSharedRef<SWidget> Widget = Super::RebuildWidget();

	RestartButton = WidgetTree->ConstructWidget<UButton>(UButton::StaticClass(), FName("RestartButton"));
	RootWidget->AddChild(RestartButton);
	UCanvasPanelSlot* RestartButtonSlot = Cast<UCanvasPanelSlot>(RestartButton->Slot);
	RestartButtonSlot->SetAnchors(FAnchors(.5f, .5f, .5f, .5f));
	RestartButtonSlot->SetSize(FVector2D(300.f, 180.f));
	RestartButtonSlot->SetAlignment(FVector2D(.5f, .5f));
	RestartButton->OnClicked.AddDynamic(this, &URestartWidget::OnRestartClicked);

	return Widget;
}

void URestartWidget::OnRestartClicked()
{
	UGameplayStatics::OpenLevel(this, FName(*()->GetName()), true);
}

in editor -

Please surround every part of the code with the code tag or else its really hard to read.

not sure what happened i will paste the code again

.h file

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

#pragma once

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


/**
 * 
 */

//class UButton;

UCLASS()
class TEST_API URestartWidget : public UUserWidget
{
	GENERATED_BODY()
protected:

	virtual void NativeConstruct() override;
	virtual TSharedRef<SWidget> RebuildWidget();


	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		class UButton * RestartButton;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	class UPanelWidget *RootWidget;

	UFUNCTION()
	void OnRestartClicked();
};

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

#include "RestartWidget.h"
#include "Runtime/UMG/Public/UMG.h"


void URestartWidget::NativeConstruct()
{
	Super::NativeConstruct();

	

}

TSharedRef<SWidget> URestartWidget::RebuildWidget()
{
	if (WidgetTree)
	{
		RootWidget = Cast<UPanelWidget>(GetRootWidget());
		if (RootWidget == nullptr)
		{
			RootWidget = WidgetTree->ConstructWidget<UCanvasPanel>(UCanvasPanel::StaticClass(), FName("RootWidget"));
			
			UCanvasPanelSlot * RootSlot = Cast<UCanvasPanelSlot>(RootWidget->Slot);
			if (RootSlot)
			{
				RootSlot->SetAnchors(FAnchors(0.f, 0.f, 1.f, 1.f));
				RootSlot->SetOffsets(FMargin(0.f, 0.f));
			}

			WidgetTree->RootWidget = RootWidget;
		}

		
	}

	TSharedRef<SWidget> Widget = Super::RebuildWidget();

	RestartButton = WidgetTree->ConstructWidget<UButton>(UButton::StaticClass(), FName("RestartButton"));
	RootWidget->AddChild(RestartButton);
	UCanvasPanelSlot* RestartButtonSlot = Cast<UCanvasPanelSlot>(RestartButton->Slot);
	RestartButtonSlot->SetAnchors(FAnchors(.5f, .5f, .5f, .5f));
	RestartButtonSlot->SetSize(FVector2D(300.f, 180.f));
	RestartButtonSlot->SetAlignment(FVector2D(.5f, .5f));
	RestartButton->OnClicked.AddDynamic(this, &URestartWidget::OnRestartClicked);

	return Widget;
}

void URestartWidget::OnRestartClicked()
{
	UGameplayStatics::OpenLevel(this, FName(*()->GetName()), true);
}

please take a look at the code below

Was a solution ever found for this?

I think you need to add BindWidget meta specifier to declare widget as part of the userwidget, since UE4 by default don’t really know for what you gonna use this pointer for, you could

UPROPERTY(EditAnywhere, BlueprintReadWrite,meta=(BindWidget))
class UButton * RestartButton;

this is not working, it’s giving following error

  1. non-optional widget binding Restart Widget not found.
  2. non-optional widget binding Root Widget not found

Afaik this meta works for widgets you manually place in UMG designer. Additionally you have to somehow specify widget name (possibly it’s c++ variable name) and set exact same name on the widget in designer. Then it should automagically put that widget in your property.

I didn’t test it tho.

Do You solve it?