Search code examples
c++qtqt5usabilityqcombobox

QCombobox - hide first item on drop-down


Hello fellow developers!

I want to realize a "Action" QCombobox where the different options trigger an action instead of choosing an option. The combobox says something like "Add Property" and when the user clicks it, he gets the different properties to choose from. If the user clicks one of them, it is added to -whateverdoesntmatter-. The combobox than switches back to it's original state.

The problem is, that the first entry "Add Property" is shown on the combobox's drop down, causing a little bit of confusion for the user since it is not really an action.

Normal state:

[Add Property]

Drop-down state:

[Add Property]
Add Property
Length
Width
etc ...

Therefore, I want to hide the first option as soon as the combobox drops down to achieve this behavior:

[Add Property]
Length
Width
etc ...

I have already tried with subclassing the combobox and overwriting showPopUp() and hidePopUp() - but I could not figure out how to get my intended behavior.

Ideas? Thanks!


Solution

  • If you don't absolutely need a QComboBox you might be better/easier off using a QToolButton with a QMenu associated and then set the button's popupMode to an appropriate value.

    You may even use a QToolBar. Simply add a QAction with your desired label (like Add Property), associate the menu to it via QAction::setMenu and at it to the toolbar. The toolbar will automatically create the tool buttons itself.

    To access the popupMode property you can use QToolBar::widgetForAction and cast it to a QToolButton.