Text not showing right in the Listview

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.)

Instead of RequestRefresh, try ForceRefresh.

They’re both UUSerwidgets and i dont find this function

Ok I found the problem. The bp implementation of your printText class needs to implement “Event On List Item Object Set”. Then the text show up correctly.

So probably an implementation in c++ of NativeOnListItemObjectSet should work. Will test it.

ListView seems to only take in entries of type blueprint so you need a BP variant, where you can just override like I showed above.

Oh and I slightly rewrote setTextTo to set DisplayString


void UWidgetPrintText::SetTextTo(FString sText)
{
    //DisplayString = sText;
    if (NameTextBlock) {
        UE_LOG(LogTemp, Error, TEXT("333string: %s"), *sText);
        FText text = FText::FromString(sText);
        DisplayString = sText;
        NameTextBlock->SetText(FText::FromString(DisplayString));                
        UE_LOG(LogTemp, Error, TEXT("111string: %s"), *sText);
        FText TextText = NameTextBlock->GetText();
        UE_LOG(LogTemp, Error, TEXT("555string: %s"), *TextText.ToString());                        
    }
}
1 Like

Sadly…but Thanks! Everything works now.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.