Skip to content

QEnumComboBox

A QComboBox with the members of a Python Enum as its items. In addition to the QComboBox API, it has:

  • enumClass() and setEnumClass(), to get and set the Enum;
  • currentEnum() and setCurrentEnum(), to get and set the current member;
  • a currentEnumChanged signal, which emits the new member.

Items are shown as str(member) if the Enum defines __str__, and otherwise as the member's name, with underscores replaced by spaces. The items can only come from the Enum: addItem, addItems, insertItem, and insertItems raise a RuntimeError.

QEnumComboBox QEnumComboBox
from enum import Enum

from qtpy.QtWidgets import QApplication

from superqt import QEnumComboBox


class SampleEnum(Enum):
    first = 1
    second = 2
    third = 3


app = QApplication([])

combo = QEnumComboBox()
combo.setEnumClass(SampleEnum)
combo.show()

app.exec()

The Enum can also be passed to the constructor:

# option A:
combo = QEnumComboBox()
combo.setEnumClass(SampleEnum)
# option B:
combo = QEnumComboBox(enum_class=SampleEnum)

Allow None

With allow_none=True, the combo box has an extra item, ----, for no member:

from enum import Enum

from superqt import QEnumComboBox


class SampleEnum(Enum):
    first = 1
    second = 2
    third = 3


# (a QApplication must exist before creating a widget)
combo = QEnumComboBox()
combo.setEnumClass(SampleEnum, allow_none=True)

When it's selected, currentEnum() returns None (and setCurrentEnum(None) selects it).

QEnumComboBox

QEnumComboBox(
    parent=None,
    enum_class: EnumMeta | None = None,
    allow_none=False,
)

ComboBox presenting options from a python Enum.

If the Enum class does not implement __str__ then a human readable name is created from the name of the enum member, replacing underscores with spaces.

Subclass of QComboBox

QEnumComboBox inherits all of QComboBox's methods, signals, and properties. Only those that superqt adds or changes are listed here.

Signals

  • currentEnumChanged(object)

Methods

ComboBox presenting options from a python Enum.

If the Enum class does not implement __str__ then a human readable name is created from the name of the enum member, replacing underscores with spaces.

addItem

addItem(*_, **__)

addItems

addItems(*_, **__)

clear

clear()

currentEnum

currentEnum() -> EnumType | None

Current value as Enum member.

enumClass

enumClass() -> EnumMeta | None

Return current Enum class.

insertItem

insertItem(*_, **__)

insertItems

insertItems(*_, **__)

isOptional

isOptional() -> bool

Return if current enum is with optional annotation.

setCurrentEnum

setCurrentEnum(value: EnumType | None) -> None

Set value with Enum.

setEnumClass

setEnumClass(enum: EnumMeta | None, allow_none=False)

Set enum class from which members value should be selected.

setInsertPolicy

setInsertPolicy(policy)