QEnumComboBox#
QEnumComboBox
is a variant of
QComboBox
that populates the items in
the combobox based on a python Enum
class. In addition to all the methods
provided by QComboBox
, this subclass adds the methods
enumClass
/setEnumClass
to get/set the current Enum
class represented by
the combobox, and currentEnum
/setCurrentEnum
to get/set the current Enum
member in the combobox. There is also a new signal currentEnumChanged(enum)
analogous to currentIndexChanged
and currentTextChanged
.
Method like insertItem
and addItem
are blocked and try of its usage will end
with RuntimeError
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_()
Another option is to use optional enum_class
argument of constructor and change
# option A:
combo = QEnumComboBox()
combo.setEnumClass(SampleEnum)
# option B:
combo = QEnumComboBox(enum_class=SampleEnum)
Allow None
#
QEnumComboBox
also allows using Optional
type annotation:
from enum import Enum
from superqt import QEnumComboBox
class SampleEnum(Enum):
first = 1
second = 2
third = 3
# as usual:
# you must create a QApplication before create a widget.
combo = QEnumComboBox()
combo.setEnumClass(SampleEnum, allow_none=True)
In this case there is added option ----
and the currentEnum()
method will
return None
when it is selected.
Qt Class#
Signals#
currentEnumChanged
#
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.
currentEnum() -> Optional[EnumType]
#
Current value as Enum member.
enumClass() -> Optional[EnumMeta]
#
Return current Enum class.
isOptional() -> bool
#
Return if current enum is with optional annotation.
setCurrentEnum(value: Optional[EnumType]) -> None
#
Set value with Enum.
setEnumClass(enum: Optional[EnumMeta], allow_none: Optional[EnumMeta] = False)
#
Set enum class from which members value should be selected.