Skip to content

EnumComboBox

Qt Base Class: QComboBox

Signature: QComboBox(self, parent: Optional[PySide6.QtWidgets.QWidget] = None) -> None

Base classes

Name Children Inherits
ComboBox
prettyqt.widgets.combobox

Subclasses

Class Module Description
CursorEdit prettyqt.custom_widgets.editors.cursoredit

⋔ Inheritance diagram

graph TD
  1473367077472["custom_widgets.EnumComboBox"]
  1473296236368["widgets.ComboBox"]
  1473296223680["widgets.ComboBoxMixin"]
  1473293688240["widgets.WidgetMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473245548480["gui.PaintDeviceMixin"]
  1473241370080["QtWidgets.QComboBox"]
  1473290849680["QtWidgets.QWidget"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473300082368["QtGui.QPaintDevice"]
  1473296236368 --> 1473367077472
  1473296223680 --> 1473296236368
  1473293688240 --> 1473296223680
  1473299815024 --> 1473293688240
  140713234304496 --> 1473299815024
  1473245548480 --> 1473293688240
  140713234304496 --> 1473245548480
  1473241370080 --> 1473296236368
  1473290849680 --> 1473241370080
  1473288842240 --> 1473290849680
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208
  1473300082368 --> 1473290849680
  1473291690208 --> 1473300082368

🛈 DocStrings

Bases: ComboBox

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.

Source code in prettyqt\custom_widgets\editors\enumcombobox.py
class EnumComboBox(widgets.ComboBox):
    """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.
    """

    # current_enum_changed = core.Signal(object)
    value_changed = core.Signal(enum.Enum)

    def __init__(self, value=None, object_name: str = "enum_combobox", **kwargs):
        self._enum_class = None
        self._allow_none = False
        super().__init__(object_name=object_name, **kwargs)
        if value is not None:
            self.set_value(value)
        # self.currentIndexChanged.connect(self._emit_signal)

    def __repr__(self):
        return get_repr(self, self.get_value())

    def set_allow_none(self, value: bool):
        self._allow_none = value

    def is_none_allowed(self) -> bool:
        return self._allow_none

    def _set_enum_class(self, enum: enum.EnumMeta | None):
        """Set enum class from which members value should be selected."""
        if enum == self._enum_class:
            return None
        self._enum_class = enum
        super().clear()
        if self._allow_none and enum is not None:
            super().addItem(NONE_STRING)
        items = [i.name.replace("_", " ") for i in self._enum_class.__members__.values()]
        super().addItems(items)

    def get_enum_class(self) -> enum.EnumMeta | None:
        """Return current Enum class."""
        return self._enum_class

    def is_optional(self) -> bool:
        """Return if current enum is with optional annotation."""
        return self._allow_none

    def clear(self):
        self._enum_class = None
        self._allow_none = False
        super().clear()

    def get_value(self) -> EnumType | None:
        """Current value as Enum member."""
        if self._enum_class is None:
            return None
        class_members = list(self._enum_class.__members__.values())
        if not self._allow_none:
            return class_members[self.currentIndex()]
        is_none = self.currentText() == NONE_STRING
        return None if is_none else class_members[self.currentIndex() - 1]

    def set_value(self, value: EnumType | None) -> None:
        """Set value with Enum."""
        if value is None:
            if not self._allow_none:
                raise ValueError(value)
            self.setCurrentIndex(0)
            return
        if not isinstance(value, enum.Enum):
            value = self._enum_class(value)
        self._set_enum_class(value.__class__)
        self.setCurrentText(value.name.replace("_", " "))

    # def _emit_signal(self):
    #     if self._enum_class is not None:
    #         self.current_enum_changed.emit(self.get_value())

    allowNone = core.Property(
        bool,
        is_none_allowed,
        set_allow_none,
        doc="Whether None is allowed as a value",
    )
    enumValue = core.Property(
        enum.Enum,
        get_value,
        set_value,
        user=True,
        doc="Currently chosen value",
    )

get_enum_class() -> enum.EnumMeta | None

Return current Enum class.

Source code in prettyqt\custom_widgets\editors\enumcombobox.py
def get_enum_class(self) -> enum.EnumMeta | None:
    """Return current Enum class."""
    return self._enum_class

get_value() -> EnumType | None

Current value as Enum member.

Source code in prettyqt\custom_widgets\editors\enumcombobox.py
def get_value(self) -> EnumType | None:
    """Current value as Enum member."""
    if self._enum_class is None:
        return None
    class_members = list(self._enum_class.__members__.values())
    if not self._allow_none:
        return class_members[self.currentIndex()]
    is_none = self.currentText() == NONE_STRING
    return None if is_none else class_members[self.currentIndex() - 1]

is_optional() -> bool

Return if current enum is with optional annotation.

Source code in prettyqt\custom_widgets\editors\enumcombobox.py
def is_optional(self) -> bool:
    """Return if current enum is with optional annotation."""
    return self._allow_none

set_value(value: EnumType | None) -> None

Set value with Enum.

Source code in prettyqt\custom_widgets\editors\enumcombobox.py
def set_value(self, value: EnumType | None) -> None:
    """Set value with Enum."""
    if value is None:
        if not self._allow_none:
            raise ValueError(value)
        self.setCurrentIndex(0)
        return
    if not isinstance(value, enum.Enum):
        value = self._enum_class(value)
    self._set_enum_class(value.__class__)
    self.setCurrentText(value.name.replace("_", " "))

⌗ Property table

Qt Property Type Doc
objectName QString
modal bool
windowModality Qt::WindowModality
enabled bool
geometry QRect
frameGeometry QRect
normalGeometry QRect
x int
y int
pos QPoint
frameSize QSize
size QSize
width int
height int
rect QRect
childrenRect QRect
childrenRegion QRegion
sizePolicy QSizePolicy
minimumSize QSize
maximumSize QSize
minimumWidth int
minimumHeight int
maximumWidth int
maximumHeight int
sizeIncrement QSize
baseSize QSize
palette QPalette
font QFont
cursor QCursor
mouseTracking bool
tabletTracking bool
isActiveWindow bool
focusPolicy Qt::FocusPolicy
focus bool
contextMenuPolicy Qt::ContextMenuPolicy
updatesEnabled bool
visible bool
minimized bool
maximized bool
fullScreen bool
sizeHint QSize
minimumSizeHint QSize
acceptDrops bool
windowTitle QString
windowIcon QIcon
windowIconText QString
windowOpacity double
windowModified bool
toolTip QString
toolTipDuration int
statusTip QString
whatsThis QString
accessibleName QString
accessibleDescription QString
layoutDirection Qt::LayoutDirection
autoFillBackground bool
styleSheet QString
locale QLocale
windowFilePath QString
inputMethodHints QFlags
editable bool
count int
currentText QString
currentIndex int
currentData QVariant
maxVisibleItems int
maxCount int
insertPolicy QComboBox::InsertPolicy
sizeAdjustPolicy QComboBox::SizeAdjustPolicy
minimumContentsLength int
iconSize QSize
placeholderText QString
duplicatesEnabled bool
frame bool
modelColumn int
allowNone bool Whether None is allowed as a value
enumValue PySide::PyObjectWrapper Currently chosen value