How to set the text of a child Label?

I have a Component with alot of Labels as children.
How can I get one Label by its index and set the text of it?

I tried:

MyComponent.getChildComponent(0).setText("aa", NotificationType::doNotSendNotification);

Which doesn’t work.

The best way is to keep the Labels in your own container, since the Component will not manage the lifetime of it’s child components. That way you can keep the Labels as the actual type.
If you want to use the getChildComponent(index), you need to down cast to the type you want to use. This can return a nullptr, if it is of a different type, so be careful:

if (auto* label = dynamic_cast<Label*>(getChildComponent (0)))
    label->setText ("aa", doNotSendNotification);
1 Like

Thank you very much!

Can I somehow choose your answer as solution?