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

Enums

CustomRole

Custom model roles.

Role

alias of CustomRole

Methods

columnCount

Returns the number of columns

data

Returns data on the given index

editorContext

Returns the context in which this table is shown.

executeAction

Execute an action

executeMapLayerAction

Execute a QgsMapLayerAction

extraColumns

Empty extra columns to announce from this model.

feature

Returns the feature attributes at given model index

fieldCol

Gets column from field index

fieldConditionalStyleChanged

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

fieldIdx

Gets field index from column

flags

Returns item flags for the index

headerData

Returns header data

idToIndex

idToIndexList

idToRow

Maps feature id to table row

layer

Returns the layer this model uses as backend.

layerCache

Returns the layer cache this model uses as backend.

loadLayer

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

prefetchColumnData

Caches the entire data for one column.

prefetchSortData

Prefetches the entire data for an expression.

reload

Reloads the model data between indices

removeRows

Remove rows

request

Gets the the feature request

resetModel

Resets the model

rowCount

Returns the number of rows

rowToId

Maps row to feature id

setData

Updates data on given index

setEditorContext

Sets the context in which this table is shown.

setExtraColumns

Empty extra columns to announce from this model.

setRequest

Set a request that will be used to fill this attribute table model.

setShowValidityState

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

showValidityState

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

sortCacheExpression

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

swapRows

Swaps two rows

Signals

finished

Emitted when the model has completely loaded all features.

modelChanged

Emitted when the model has been changed.

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

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

Returns the number of columns

Parameters:

parent (QModelIndex = QModelIndex()) – parent index

Return type:

int

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]

Emitted when the model has completely loaded all features.

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

Returns item flags for the index

Parameters:

index (QModelIndex) – model index

Return type:

Qt.ItemFlags

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]

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]

Emitted when the model has been changed.

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

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()

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

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