ComboBox does not respect setWantsKeyboardFocus(false)

While adding improved keyboard handling I notices that ComboBoxes still receive keyboard focus even after setWantsKeyboardFocus(false) has been called.

Is this a bug or do ComboBoxes need special treatment to not receive keyboard focus?

If you give them an editable text label then they'll automatically enable keyboard focus, because obviously they need it for that.

I don't think I am doing that. Here is the kind of setup code I use for a ComboBox:

m_combo.addItem("Item 1", 1);
m_combo.addItem("Item 2", 2);
m_combo.addItem("Item 3", 3);

With that setup it still receives focus.

What I do in these cases is step into “m_combo.setWantsKeyboardFocus(false);” in the debugger and set a breakpoint with the condition “this==[m_combo’s current this-pointer]”. If the Combobox’s keyboard focus is changed you’ll see from where this happens.

Of course, this won’t work if it’s actually something else like the label that gets the focus and responds to it.

Thanks! This is a good way to easily track what's going on.

Everytime parentHierarchyChanged() is called on the ComboBox it calls lookAndFeelChanged() which in turn calls setWantsKeyboardFocus:

setWantsKeyboardFocus (! label->isEditable());

As far as I can see there is no way to have a ComboBox not receive focus. The parent hierarchy of a combo box changes all the time. E.g. if you add any control after the ComboBox was added.

lookAndFeelChanged() is virtual; you could inherit from ComboBox and overwrite it.


I would prefer to have a way to disable focus for ComboBoxes without having to subclass it though.

Also, I still wonder if the side effect of reseting setWantsKeyboardFocus in lookAndFeelChanged is intended.

Hopefully Jules can clarify.


So there is no other way but to subclass?