Skip to content

ColumnTableModel

Qt Base Class: QAbstractTableModel

Signature: QAbstractTableModel(self, parent: Optional[PySide6.QtCore.QObject] = None) -> None

Base classes

Name Children Inherits
ColumnItemModelMixin
prettyqt.itemmodels.columnitemmodel
AbstractTableModel
prettyqt.core.abstracttablemodel

Subclasses

Class Module Description
LogRecordModel prettyqt.itemmodels.logrecordmodel
ActionsModel prettyqt.itemmodels.actionsmodel
StorageInfoModel prettyqt.itemmodels.storageinfomodel
ShortcutsModel prettyqt.itemmodels.shortcutsmodel

⋔ Inheritance diagram

graph TD
  1473245684144["itemmodels.ColumnTableModel"]
  1473245682192["itemmodels.ColumnItemModelMixin"]
  140713234304496["builtins.object"]
  1473299901888["core.AbstractTableModel"]
  1473299900912["core.AbstractTableModelMixin"]
  1473299890176["core.AbstractItemModelMixin"]
  1473299815024["core.ObjectMixin"]
  1473289054032["QtCore.QAbstractTableModel"]
  1473289050128["QtCore.QAbstractItemModel"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473245682192 --> 1473245684144
  140713234304496 --> 1473245682192
  1473299901888 --> 1473245684144
  1473299900912 --> 1473299901888
  1473299890176 --> 1473299900912
  1473299815024 --> 1473299890176
  140713234304496 --> 1473299815024
  1473289054032 --> 1473299901888
  1473289050128 --> 1473289054032
  1473288842240 --> 1473289050128
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: ColumnItemModelMixin, AbstractTableModel

Source code in prettyqt\itemmodels\columnitemmodel.py
class ColumnTableModel(ColumnItemModelMixin, core.AbstractTableModel):
    def __init__(
        self,
        items: Sequence,
        columns: Sequence[ColumnItem],
        mime_type: str | None = None,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self.items = items
        self.mime_type = mime_type
        self._attr_cols = []
        self.set_columns(columns)

    def rowCount(self, parent: core.ModelIndex | None = None) -> int:
        parent = parent or core.ModelIndex()
        return 0 if parent.isValid() else len(self.items)

    def columnCount(self, parent: core.ModelIndex | None = None) -> int:
        parent = parent or core.ModelIndex()
        return 0 if parent.isValid() else len(self._attr_cols)

    def data_by_index(self, index: core.ModelIndex):
        return self.items[index.row()]

    def setData(
        self,
        index: core.ModelIndex,
        value: Any,
        role: constants.ItemDataRole = constants.EDIT_ROLE,
    ):
        if role == constants.USER_ROLE:
            self.items[index.row()] = value
            self.update_row(index.row())
            return True
        return super().setData(index, value, role)

    def removeRows(self, row: int, count: int, parent):
        end_row = row + count - 1
        with self.remove_rows(row, end_row, parent):
            for i in range(end_row, row - 1, -1):
                self.items.pop(i)
        return True

    def dropMimeData(self, mime_data, action, row, column, parent_index):
        if not mime_data.hasFormat(self.mime_type):
            return False
        # Since we only drop in between items, parent_index must be invalid,
        # and we use the row arg to know where the drop took place.
        if parent_index.isValid():
            return False
        indexes = mime_data.get_json_data(self.mime_type)
        pos = row if row < len(self.items) and row != -1 else len(self.items)
        rem_offset = sum(i <= pos for i in indexes)
        new = [self.items[i] for i in indexes]
        with self.change_layout():
            for i in sorted(indexes, reverse=True):
                self.items.pop(i)
            for item in reversed(new):
                self.items.insert(pos - rem_offset, item)
        return False

    def sort(self, ncol: int, order):
        """Sort table by given column number."""
        is_asc = order == constants.ASCENDING
        if sorter := self._attr_cols[ncol].label:
            with self.change_layout():
                self.items.sort(key=sorter, reverse=is_asc)

    def add(self, item: Any, position: int | None = None):
        """Append provided item to the list."""
        self.add_items(items=[item], position=position)
        return item

    def add_items(self, items: Iterable[Any], position: int | None = None):
        """Append a list of items to the list."""
        if position is None:
            position = len(self.items)
        items = list(items)
        with self.insert_rows(position, position + len(items) - 1):
            for i, _ in enumerate(items):
                self.items.insert(i + position, items[i])
            # self.items.extend(items)
        return items

    def remove_items(self, offsets: Iterable[int]):
        for offset in sorted(offsets, reverse=True):
            self.removeRow(offset)

add(item: Any, position: int | None = None)

Append provided item to the list.

Source code in prettyqt\itemmodels\columnitemmodel.py
def add(self, item: Any, position: int | None = None):
    """Append provided item to the list."""
    self.add_items(items=[item], position=position)
    return item

add_items(items: Iterable[Any], position: int | None = None)

Append a list of items to the list.

Source code in prettyqt\itemmodels\columnitemmodel.py
def add_items(self, items: Iterable[Any], position: int | None = None):
    """Append a list of items to the list."""
    if position is None:
        position = len(self.items)
    items = list(items)
    with self.insert_rows(position, position + len(items) - 1):
        for i, _ in enumerate(items):
            self.items.insert(i + position, items[i])
        # self.items.extend(items)
    return items

sort(ncol: int, order: int)

Sort table by given column number.

Source code in prettyqt\itemmodels\columnitemmodel.py
def sort(self, ncol: int, order):
    """Sort table by given column number."""
    is_asc = order == constants.ASCENDING
    if sorter := self._attr_cols[ncol].label:
        with self.change_layout():
            self.items.sort(key=sorter, reverse=is_asc)

⌗ Property table

Qt Property Type Doc
objectName QString