Class: QgsAttributeTableModel

A model backed by a QgsVectorLayerCache which is able to provide feature/attribute information to a QAbstractItemView.

Is able to generate editor widgets for its QModelIndexes as well. Is mostly referred to as “master model” within this doc and the source.

see <a href=”http://doc.qt.digia.com/qt/model-view-programming.html”>Qt Model View Programming</a>

Class Hierarchy

Inheritance diagram of qgis.gui.QgsAttributeTableModel

Base classes

QAbstractTableModel

QAbstractItemModel

QObject

class qgis.gui.QgsAttributeTableModel[source]

Bases: QAbstractTableModel

__init__(layerCache: QgsVectorLayerCache | None, parent: QObject | None = None)

Constructor

Parameters:
  • layerCache (Optional[QgsVectorLayerCache]) – A layer cache to use as backend

  • parent (Optional[QObject] = None) – The parent QObject (owner)

class CustomRole(*values)

Bases: IntEnum

Custom model roles.

Note

Prior to QGIS 3.36 this was available as QgsAttributeTableModel.Role

Added in version 3.36.

  • FeatureId: Get the feature id of the feature in this row

    Available as QgsAttributeTableModel.FeatureIdRole in older QGIS releases.

  • FieldIndex: Get the field index of this column

    Available as QgsAttributeTableModel.FieldIndexRole in older QGIS releases.

  • User: Start further roles starting from this role

    Available as QgsAttributeTableModel.UserRole in older QGIS releases.

  • Sort: Role used for sorting start here

    Available as QgsAttributeTableModel.SortRole in older QGIS releases.

Role

alias of CustomRole

virtual columnCount(self, parent: QModelIndex = QModelIndex()) int[source]

Returns the number of columns

Parameters:

parent (QModelIndex = QModelIndex()) – parent index

Return type:

int

virtual data(self, index: QModelIndex, role: int) Any[source]

Returns data on the given index

Parameters:
  • index (QModelIndex) – model index

  • role (int) – data role

Return type:

Any

editorContext(self) QgsAttributeEditorContext

Returns the context in which this table is shown. Will be forwarded to any editor widget created when editing data on this model.

Return type:

QgsAttributeEditorContext

Returns:

The context

executeAction(self, action: QUuid, idx: QModelIndex)[source]

Execute an action

Parameters:
  • action (QUuid)

  • idx (QModelIndex)

executeMapLayerAction(self, action: QgsMapLayerAction | None, idx: QModelIndex, context: QgsMapLayerActionContext = QgsMapLayerActionContext())[source]

Execute a QgsMapLayerAction

Parameters:
extraColumns(self) int[source]

Empty extra columns to announce from this model. Any extra columns need to be implemented by proxy models in front of this model.

Return type:

int

feature(self, idx: QModelIndex) QgsFeature[source]

Returns the feature attributes at given model index

Return type:

QgsFeature

Returns:

feature attributes at given model index

Parameters:

idx (QModelIndex)

fieldCol(self, idx: int) int[source]

Gets column from field index

Parameters:

idx (int)

Return type:

int

fieldConditionalStyleChanged(self, fieldName: str | None)[source]

Handles updating the model when the conditional style for a field changes.

Parameters:

fieldName (Optional[str]) – name of field whose conditional style has changed

fieldIdx(self, col: int) int[source]

Gets field index from column

Parameters:

col (int)

Return type:

int

signal finished[source]

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

virtual flags(self, index: QModelIndex) Qt.ItemFlags[source]

Returns item flags for the index

Parameters:

index (QModelIndex) – model index

Return type:

Qt.ItemFlags

virtual headerData(self, section: int, orientation: Qt.Orientation, role: int = Qt.DisplayRole) Any[source]

Returns header data

Parameters:
  • section (int) – required section

  • orientation (Qt.Orientation) – horizontal or vertical orientation

  • role (int = Qt.DisplayRole) – data role

Return type:

Any

idToIndex(self, id: int) QModelIndex[source]
Parameters:

id (int)

Return type:

QModelIndex

idToIndexList(self, id: int) List[QModelIndex][source]
Parameters:

id (int)

Return type:

List[QModelIndex]

idToRow(self, id: int) int[source]

Maps feature id to table row

Parameters:

id (int) – feature id

Return type:

int

layer(self) QgsVectorLayer | None[source]

Returns the layer this model uses as backend. Retrieved from the layer cache.

Return type:

Optional[QgsVectorLayer]

layerCache(self) QgsVectorLayerCache | None[source]

Returns the layer cache this model uses as backend.

Return type:

Optional[QgsVectorLayerCache]

virtual loadLayer(self)[source]

Loads the layer into the model Preferably to be called, before using this model as source for any other proxy model

signal modelChanged[source]

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

prefetchColumnData(self, column: int)[source]

Caches the entire data for one column. This should be called prior to sorting, so the data does not have to be fetched for every single comparison. Specify -1 as column to invalidate the cache

Parameters:

column (int) – The column index of the field to catch

prefetchSortData(self, expression: str | None, cacheIndex: int = 0)[source]

Prefetches the entire data for an expression. Based on this cached information the sorting can later be done in a performant way. A cacheIndex can be specified if multiple caches should be filled. In this case, the caches will be available as QgsAttributeTableModel.SortRole + cacheIndex.

Parameters:
  • expression (Optional[str])

  • cacheIndex (int = 0)

reload(self, index1: QModelIndex, index2: QModelIndex)[source]

Reloads the model data between indices

Parameters:
  • index1 (QModelIndex) – start index

  • index2 (QModelIndex) – end index

virtual removeRows(self, row: int, count: int, parent: QModelIndex = QModelIndex()) bool[source]

Remove rows

Parameters:
  • row (int)

  • count (int)

  • parent (QModelIndex = QModelIndex())

Return type:

bool

request(self) QgsFeatureRequest

Gets the the feature request

Return type:

QgsFeatureRequest

resetModel(self)[source]

Resets the model

Alias to loadLayer()

virtual rowCount(self, parent: QModelIndex = QModelIndex()) int[source]

Returns the number of rows

Parameters:

parent (QModelIndex = QModelIndex()) – parent index

Return type:

int

rowToId(self, row: int) int[source]

Maps row to feature id

Parameters:

row (int) – row number

Return type:

int

virtual setData(self, index: QModelIndex, value: Any, role: int = Qt.EditRole) bool[source]

Updates data on given index

Parameters:
  • index (QModelIndex) – model index

  • value (Any) – new data value

  • role (int = Qt.EditRole) – data role

Return type:

bool

setEditorContext(self, context: QgsAttributeEditorContext)[source]

Sets the context in which this table is shown. Will be forwarded to any editor widget created when editing data on this model.

Parameters:

context (QgsAttributeEditorContext) – The context

setExtraColumns(self, extraColumns: int)[source]

Empty extra columns to announce from this model. Any extra columns need to be implemented by proxy models in front of this model.

Parameters:

extraColumns (int)

setRequest(self, request: QgsFeatureRequest)[source]

Set a request that will be used to fill this attribute table model. In contrast to a filter, the request will constrain the data shown without the possibility to dynamically adjust it.

Parameters:

request (QgsFeatureRequest) – The request to use to fill this table model.

setShowValidityState(self, show: bool)[source]

Sets whether the attribute table will add a visual feedback to cells when an attribute constraint is not met.

Added in version 3.30.

Parameters:

show (bool)

showValidityState(self) bool[source]

Returns whether the attribute table will add a visual feedback to cells when an attribute constraint is not met.

Added in version 3.30.

Return type:

bool

sortCacheExpression(self, cacheIndex: int = 0) str[source]

The expression which was used to fill the sorting cache at index cacheIndex.

Parameters:

cacheIndex (int = 0)

Return type:

str

swapRows(self, a: int, b: int)[source]

Swaps two rows

Parameters:
  • a (int) – first row

  • b (int) – second row