Disconnecting signals from menu items?

Started by Null_Ptr, 29 March 2023, 16:25:11

Null_Ptr

I've been searching through the documentation on signal events with MenuBar items and have been unable to find anything on disconnecting them (could only find this option for other widgets). The problem is I have menu items that could at times have the text change which causes the connected signal to no longer work. If there is a way to preserve the signal after using MenuBar::changeMenuItem() that would work fine for my purposes too.
Thanks!

texus

You can disconnect the signal like this:
Code (cpp) Select
unsigned int callbackId = menu->connectMenuItem(...);
menu->onMenuItemClick.disconnect(callbackId);

This is because the connectMenuItem is a simple wrapper function that connects to the onMenuItemClick and when a menu item is clicked it will check whether it needs to call your callback function or not based on whether the item name matched.

Alternatively, instead of using connectMenuItem to connect multiple callbacks and deal with disconnection, you could just use onMenuItemClick directy (which is triggered for any menu item click).
Code (cpp) Select
menu->onMenuItemClick([](const std::vector<String>& clickedMenuItem){
    if ((clickedMenuItem[0] == "File) && (clickedMenuItem[1] == "Load")) {
        ...
    }
    else if (...) {
       ...
    }
});

Null_Ptr

Perfect exactly what I needed. Thanks again