QEnumComboBox¶
A QComboBox with the members of a
Python Enum as its items. In addition to the QComboBox API, it has:
enumClass()andsetEnumClass(), to get and set theEnum;currentEnum()andsetCurrentEnum(), to get and set the current member;- a
currentEnumChangedsignal, 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.
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
¶
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.
setEnumClass
¶
Set enum class from which members value should be selected.