TreeViewItem createItemComponent() & itemClicked()?

Hi,

I have a question about a TreeView and its TreeViewItem.
I am using TreeViewItem::createItemComponent() in order to create a custom item component.

This custom component is intercepting mouse clicks.

Therefore I pass a reference of the TreeViewItem to the custom component.
And in the custom components mouseDown method, I call treeViewItemRef->itemClicked(). So that the item knows, when its clicked and can perform necessary actions.

Thats all working fine. No problems.

BUT I recently read this comment in the JUCE documentation of TreeViewItem:: createItemComponent:
“Also bear in mind that if the component keeps a reference to the item that created it, that item could be deleted before the component”
https://docs.juce.com/master/classTreeViewItem.html#af821a303a31a9256e00b94ff8fe2595d

So, do I read this right? Seems I am not allowed, to pass the custom component a reference to the ValueTreeItem, which created it? Because the item could get deleted any time, even though the custom component is still alive?

But that confuses me. How can I then notify the ValueTreeItem, that it has been clicked?
Is there another option?
How are you guys doing it?

1 Like