Download

Inherit From UUserWidget how? C++

hi.
since i want to use UI Widget by C++ code, i’m trying to inherit my Main Menu class from UUserWidget class…i also implemented the UUserWidget constructor but when i’m trying to instantiate something ( in this case Canvas Panel ) it won’t.

what i missing up?

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

#pragma once

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

/**
 * 
 */
UCLASS()
class BYEGG_API UMainMenuBase : public UUserWidget
{
	GENERATED_BODY()
	
	UMainMenuBase(const FObjectInitializer& ObjectInitializer);

	// Variable
	UPROPERTY()
	UCanvasPanel* Panel;
	
	// Function
	UFUNCTION(BlueprintCallable)
	void OnStartGamePress();
};

and .cpp is

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


#include "MainMenuBase.h"

UMainMenuBase::UMainMenuBase(const FObjectInitializer& ObjectInitializer)
	:Super(ObjectInitializer)
{
	Panel =
		CreateDefaultSubobject<UCanvasPanel>(TEXT("Main_Panel"));
}

void UMainMenuBase::OnStartGamePress()
{
	
}

Widgets are probably incompatible with CreateDefaultSubobject. They aren’t even really compatible with NewObject, you have to call CreateWidget. And you probably can’t call CreateWidget during the constructor.

More generally however, you don’t really want to create widgets this way. Now that you’ve got your MainMenuBase native class you should run the editor and create a blueprint that derives from it and setup all your widgets in the Design View.

If you really need the native class to have a pointer to one of the widgets like that (hint: you probably don’t, but it’s also not entirely wrong) you can add meta = (BindWidget) to the UPROPERTY macro. That will require any blueprints that derive from your native class to have a widget of that type with that name and the variable will be filled in automatically for you when the widget is constructed.

thank’s i definitly slved my issue by going wit the C++ native clus + Blueprint, i just needed to put my login on button click event under c++ instead blueprint…thank’s