ComboBox, setText() fails if add items to the PopupMenu that use a lambda
auto& m = *box.getRootMenu();
m.addItem (1, "a");
m.addItem ("b", [] {});
m.addItem (2, "c");
m.addItem ("d", [] {});
m.addItem (3, "e");
m.addItem ("f", [] {});
Now if you call setText("d")
or setText("f")
the ComboBox text will be set to "b"
Code to reproduce:
#include "MainComponent.h"
//==============================================================================
MainComponent::MainComponent()
{
startTimer (1000);
setSize (600, 400);
addAndMakeVisible(box);
auto& m = *box.getRootMenu();
m.addItem (1, "a");
m.addItem ("b", [] {});
m.addItem (2, "c");
m.addItem ("d", [] {});
m.addItem (3, "e");
m.addItem ("f", [] {});
}
MainComponent::~MainComponent()
{
This file has been truncated. show original
reuk
August 23, 2022, 6:40pm
2
I’m not sure whether this should be expected to work as written. The ComboBox requires each item to have a unique non-zero ID, but you’re not specifying an ID for the items with lambdas.
This works:
m.addItem (1, "a");
m.addItem (juce::PopupMenu::Item ("b").setID (2).setAction ([] {DBG ("b");}));
m.addItem (3, "c");
m.addItem (juce::PopupMenu::Item ("d").setID (4).setAction ([] {DBG ("d");}));
m.addItem (5, "e");
m.addItem (juce::PopupMenu::Item ("f").setID (6).setAction ([] {DBG ("f");}));
Ok, that makes sense I guess. I wouldn’t have expected lambda menu items to have a valid id.
reFX
August 23, 2022, 8:09pm
4
Would it be too much to ask to add a convenience function, so we can easily add items with an ID and a lambda?
m.addItem (1, "a", [] {});
Please?
2 Likes