Skip to content

DTypeDelegate

Base classes

Name Children Inherits
StyledItemDelegate
prettyqt.widgets.styleditemdelegate

⋔ Inheritance diagram

graph TD
  4202167726["dtypedelegate.DTypeDelegate"]
  4109708588["widgets.StyledItemDelegate"]
  3153451512["widgets.AbstractItemDelegateMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  4201154414["QtWidgets.QStyledItemDelegate"]
  3961416811["QtWidgets.QAbstractItemDelegate"]
  13951691["QtCore.QObject"]
  395147830["sip.wrapper"]
  46922758["sip.simplewrapper"]
  4109708588 --> 4202167726
  3153451512 --> 4109708588
  3227531497 --> 3153451512
  2751680898 --> 3227531497
  4201154414 --> 4109708588
  3961416811 --> 4201154414
  13951691 --> 3961416811
  395147830 --> 13951691
  46922758 --> 395147830
  2751680898 --> 46922758

🛈 DocStrings

Bases: StyledItemDelegate

Edits a dtype with an editable combo box (each entry with its dtype icon).

The chosen text is written back with setData(index, text, role); the model converts the column (PandasColumnListModel does astype). A failed conversion leaves the column unchanged.

Source code in src/cutepandas/delegates/dtypedelegate.py
class DTypeDelegate(widgets.StyledItemDelegate):
    """Edits a dtype with an editable combo box (each entry with its dtype icon).

    The chosen text is written back with `setData(index, text, role)`; the model
    converts the column (`PandasColumnListModel` does `astype`). A failed conversion
    leaves the column unchanged.
    """

    ID = "dtype"

    def __init__(
        self,
        dtypes: Sequence[str] = PANDAS_DTYPES,
        role: constants.ItemDataRole = constants.EDIT_ROLE,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self.dtypes = list(dtypes)
        self.role = role

    def createEditor(self, parent, option, index: core.ModelIndex):
        combo = widgets.ComboBox(parent)
        combo.set_editable(True)
        for dtype in self.dtypes:
            try:
                icon = icons.icon_for_dtype(dtype)
            except TypeError, ValueError:
                icon = None
            combo.add(dtype, icon=icon)
        combo.activated.connect(lambda _: self.commitData.emit(combo))
        return combo

    def setEditorData(self, editor, index: core.ModelIndex):
        editor.setCurrentText(str(index.data(constants.DISPLAY_ROLE)))

    def setModelData(self, editor, model, index: core.ModelIndex):
        text = editor.currentText().strip()
        if text and text != index.data(constants.DISPLAY_ROLE):
            model.setData(index, text, self.role)

:fa-brands-github: Show source on GitHub