EnumComboBox

EnumComboBox<T> is a QComboBox subclass that auto-populates its items from a Q_ENUM-registered enumeration at construction time. It is used in Blackchirp wherever a combo box must reflect a scoped or unscoped enum whose enumerators should be presented to the user without a hand-written population loop.

The constructor calls QMetaEnum::fromType<T>() to obtain the enum’s metadata, iterates over every enumerator, and adds one item per enumerator. The display label is the enumerator’s string name with underscores replaced by spaces. The integer value of the enumerator is stored as each item’s itemData so that currentValue() and setCurrentValue() can work without needing to know index positions.

The type parameter T must be declared with Q_ENUM (inside a QObject-derived class) or Q_ENUM_NS (in a namespace). If QMetaEnum::fromType<T>() returns an invalid meta-enum — which happens when T has no Q_ENUM registration — the constructor adds no items and the combo box is empty.

Item access

Beyond the standard QComboBox index-based API, EnumComboBox provides two methods that return the underlying QStandardItem* for a given entry. These are useful when individual items need to be disabled (grayed out) or given a custom foreground color:

  • itemForValue(T v) — looks up the item by enum value.

  • itemAt(int i) — looks up the item by row index.

Both return nullptr when the requested entry does not exist or when the combo box model is not a QStandardItemModel.

API Reference

template<typename T>
class EnumComboBox : public QComboBox

Type-safe combo box that auto-populates from a Q_ENUM-registered enumeration.

The constructor uses QMetaEnum::fromType<T>() to iterate over every enumerator in T, adds one item per enumerator with its name as the display label (underscores replaced by spaces) and its integer value stored as item data, and maintains type-safe accessors. T must be declared with Q_ENUM or Q_ENUM_NS; otherwise QMetaEnum::fromType<T>() returns an empty meta-object and the combobox contains no items.

Public Functions

inline EnumComboBox(QWidget *parent = nullptr)

Construct the combo box and populate it from the enumerators of T.

inline virtual ~EnumComboBox()
inline T value(int i) const

Return the enum value stored for item at index i.

inline T currentValue() const

Return the enum value of the currently selected item.

inline void setCurrentValue(T v)

Select the item whose stored enum value equals v.

Has no effect if v is not present in the combo box.

inline QStandardItem *itemForValue(T v)

Return the QStandardItem for the row whose enum value equals v, or nullptr.

Callers can use the returned item to disable or restyle individual entries.

inline QStandardItem *itemAt(int i)

Return the QStandardItem at row index i, or nullptr if out of range.

Callers can use the returned item to disable or restyle individual entries.