Adding listener to valuetree child does not work

Hi guys,

I’m trying to add a ValueTree::Listener to a valuetree.
This valuetree is a child of another valuetree.

The problem is that when I add the listener like childValueTree.addListener(listener), it works good. But when I do like parent.getChild(0).addListener(listener), it does not work.
Comparing the two as in childValueTree == parent.getChild(0) returns true.

Does anyone have an idea why this stuff is happening?

Thank you!
Jelle

Listeners are local to the “shell” that is the ValueTree - they don’t get copied around.

As ValueTree::addListener's documentation states:

/** Adds a listener to receive callbacks when this tree is changed in some way.

    The listener is added to this specific ValueTree object, and not to the shared
    object that it refers to. When this object is deleted, all the listeners will
    be lost, even if other references to the same ValueTree still exist. And if you
    use the operator= to make this refer to a different ValueTree, any listeners will
    begin listening to changes to the new tree instead of the old one.

    When you're adding a listener, make sure that you add it to a ValueTree instance that
    will last for as long as you need the listener. In general, you'd never want to add a
    listener to a local stack-based ValueTree, and would usually add one to a member variable.

    @see removeListener
*/
1 Like

Ah thank you, that’s clear to me!