MainWidget.cpp add item:
UUserWidget* PrintTextWidget = CreateWidget<UUserWidget>(this, PrintTextWidgetClass);
UWidgetPrintText* PrintText = Cast<UWidgetPrintText>(PrintTextWidget);
if(PrintText && PrintTextWidget){
FString test = "Hi";
UE_LOG(LogTemp, Error, TEXT("444string: %s"), *test);
PrintText->SetTextTo(test);
LVRed->AddItem(PrintText);
LVRed->RequestRefresh();
}
Complete WidgetPrintText.h:
public:
FString DisplayString;
UPROPERTY(meta = (BindWidget)) //this methode also used in mainwidget.cpp
class UTextBlock* NameTextBlock;
void SetTextTo(FString sText);
protected:
void NativeConstruct() override;
Complete WidgetPrintText.cpp
void UWidgetPrintText::NativeConstruct()
{
Super::NativeConstruct();
if(NameTextBlock){
//SetTextTo(DisplayString);
UE_LOG(LogTemp, Error, TEXT("222string: %s"), *DisplayString);
APlayerController* ThisPlayer = GetOwningPlayer();
//NameTextBlock->SetText(FText::FromString(DisplayString));
SetTextTo(DisplayString);
}
}
void UWidgetPrintText::SetTextTo(FString sText)
{
//DisplayString = sText;
if(NameTextBlock){
UE_LOG(LogTemp, Error, TEXT("333string: %s"), *sText);
NameTextBlock->SetText(FText::FromString(sText));
UE_LOG(LogTemp, Error, TEXT("111string: %s"), *sText);
FText TextText = NameTextBlock->GetText();
UE_LOG(LogTemp, Error, TEXT("555string: %s"), *TextText.ToString());
}
}
//if i change the text NativeConstruct it works, if i change the text in SetTextTo it doesnt work
Output Log:
LogTemp: Error: 444string: Hi
LogTemp: Error: 333string: Hi
LogTemp: Error: 111string: Hi
LogTemp: Error: 555string: Hi
LogTemp: Error: ButtonClicked!
LogTemp: Error: 222string:
LogTemp: Error: 333string:
LogTemp: Error: 111string:
LogTemp: Error: 555string:
Something I noticed:
-Removing NativeConstruct dont work
-Removing SetTextTo(DisplayString); in NativeConstruct didnt work
-NativeConstruct or else functions removing or doesnt get any variable and default text just apears after adding the Text
-If you look at the Logs, for somereason at the begining everything accesses “Hi” from MainWidget.cpp but after i have added this widget, NativeConstruct deletes or doesnt get it anymore and overwrite the text again…
Thanks for any help! I’m already working half a year with UE5 and without breaks learning with Courses or my Own projects I do while watching some tutorials. I’m still figuring out how to understand the official Documentation of UE5.+. (did i asked chatgpt for help and wasted 2 days? yes.)