QgsAttributeTableModel class

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.

Public types

enum Role { FeatureIdRole = Qt::UserRole, FieldIndexRole, UserRole, SortRole }

Constructors, destructors, conversion operators

QgsAttributeTableModel(QgsVectorLayerCache* layerCache, QObject* parent = nullptr)
Constructor.

Public functions

auto columnCount(const QModelIndex& parent = QModelIndex()) const -> int override
Returns the number of columns.
auto data(const QModelIndex& index, int role) const -> QVariant override
Returns data on the given index.
auto editorContext() const -> const QgsAttributeEditorContext&
Returns the context in which this table is shown.
void executeAction(QUuid action, const QModelIndex& idx) const
Execute an action.
void executeMapLayerAction(QgsMapLayerAction* action, const QModelIndex& idx) const
Execute a QgsMapLayerAction.
auto extraColumns() const -> int
Empty extra columns to announce from this model.
auto feature(const QModelIndex& idx) const -> QgsFeature
Returns the feature attributes at given model index.
auto fieldCol(int idx) const -> int
Gets column from field index.
auto fieldIdx(int col) const -> int
Gets field index from column.
auto flags(const QModelIndex& index) const -> Qt::ItemFlags override
Returns item flags for the index.
auto headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const -> QVariant override
Returns header data.
auto idToRow(QgsFeatureId id) const -> int
Maps feature id to table row.
auto layer() const -> QgsVectorLayer*
Returns the layer this model uses as backend.
auto layerCache() const -> QgsVectorLayerCache*
Returns the layer cache this model uses as backend.
void prefetchColumnData(int column)
Caches the entire data for one column.
void prefetchSortData(const QString& expression, unsigned long cacheIndex = 0)
Prefetches the entire data for an expression.
void reload(const QModelIndex& index1, const QModelIndex& index2)
Reloads the model data between indices.
auto removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) -> bool override
Remove rows.
auto request() const -> const QgsFeatureRequest&
Gets the the feature request.
void resetModel()
Resets the model.
auto rowCount(const QModelIndex& parent = QModelIndex()) const -> int override
Returns the number of rows.
auto rowToId(int row) const -> QgsFeatureId
Maps row to feature id.
auto setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) -> bool override
Updates data on given index.
void setEditorContext(const QgsAttributeEditorContext& context)
Sets the context in which this table is shown.
void setExtraColumns(int extraColumns)
Empty extra columns to announce from this model.
void setRequest(const QgsFeatureRequest& request)
Set a request that will be used to fill this attribute table model.
auto sortCacheExpression(unsigned long cacheIndex = 0) const -> QString
The expression which was used to fill the sorting cache at index cacheIndex.
void swapRows(QgsFeatureId a, QgsFeatureId b)
Swaps two rows.

Signals

void modelChanged()
Model has been changed.
void progress(int i, bool& cancel)

Public slots

void fieldConditionalStyleChanged(const QString& fieldName)
Handles updating the model when the conditional style for a field changes.
void loadLayer() virtual
Loads the layer into the model Preferably to be called, before using this model as source for any other proxy model.

Private functions

void loadAttributes() virtual
Gets mFieldCount, mAttributes.
auto loadFeatureAtId(QgsFeatureId fid) const -> bool virtual
Load feature fid into local cache (mFeat)

Private slots

void attributeDeleted(int idx) virtual
Called whenever a column is removed;.
void attributeValueChanged(QgsFeatureId fid, int idx, const QVariant& value) virtual
Launched when attribute value has been changed.
void editCommandEnded() virtual
Gets called when an edit command ends This will synchronize all fields which have been changed since the last edit command in one single go.
void featureAdded(QgsFeatureId fid, bool resettingModel = false) virtual
Launched when a feature has been added.
void featuresDeleted(const QgsFeatureIds& fids) virtual
Launched when features have been deleted.
void layerDeleted() virtual
Launched when layer has been deleted.
void updatedFields() virtual
Launched whenever the number of fields has changed.

Enum documentation

enum QgsAttributeTableModel::Role

Enumerators
FeatureIdRole

Get the feature id of the feature in this row.

FieldIndexRole

Get the field index of this column.

UserRole

Start further roles starting from this role.

SortRole

Roles used for sorting start here.

Function documentation

QgsAttributeTableModel::QgsAttributeTableModel(QgsVectorLayerCache* layerCache, QObject* parent = nullptr)

Constructor.

Parameters
layerCache A layer cache to use as backend
parent The parent QObject (owner)

int QgsAttributeTableModel::columnCount(const QModelIndex& parent = QModelIndex()) const override

Returns the number of columns.

Parameters
parent parent index

QVariant QgsAttributeTableModel::data(const QModelIndex& index, int role) const override

Returns data on the given index.

Parameters
index model index
role data role

const QgsAttributeEditorContext& QgsAttributeTableModel::editorContext() const

Returns the context in which this table is shown.

Returns The context

Will be forwarded to any editor widget created when editing data on this model.

int QgsAttributeTableModel::extraColumns() const

Empty extra columns to announce from this model.

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

QgsFeature QgsAttributeTableModel::feature(const QModelIndex& idx) const

Returns the feature attributes at given model index.

Returns feature attributes at given model index

Qt::ItemFlags QgsAttributeTableModel::flags(const QModelIndex& index) const override

Returns item flags for the index.

Parameters
index model index

QVariant QgsAttributeTableModel::headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override

Returns header data.

Parameters
section required section
orientation horizontal or vertical orientation
role data role

int QgsAttributeTableModel::idToRow(QgsFeatureId id) const

Maps feature id to table row.

Parameters
id feature id

QgsVectorLayer* QgsAttributeTableModel::layer() const

Returns the layer this model uses as backend.

Retrieved from the layer cache.

void QgsAttributeTableModel::prefetchColumnData(int column)

Caches the entire data for one column.

Parameters
column The column index of the field to catch

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

void QgsAttributeTableModel::prefetchSortData(const QString& expression, unsigned long cacheIndex = 0)

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.

void QgsAttributeTableModel::reload(const QModelIndex& index1, const QModelIndex& index2)

Reloads the model data between indices.

Parameters
index1 start index
index2 end index

void QgsAttributeTableModel::resetModel()

Resets the model.

Alias to loadLayer()

int QgsAttributeTableModel::rowCount(const QModelIndex& parent = QModelIndex()) const override

Returns the number of rows.

Parameters
parent parent index

QgsFeatureId QgsAttributeTableModel::rowToId(int row) const

Maps row to feature id.

Parameters
row row number

bool QgsAttributeTableModel::setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override

Updates data on given index.

Parameters
index model index
value new data value
role data role

void QgsAttributeTableModel::setEditorContext(const QgsAttributeEditorContext& context)

Sets the context in which this table is shown.

Parameters
context The context

Will be forwarded to any editor widget created when editing data on this model.

void QgsAttributeTableModel::setExtraColumns(int extraColumns)

Empty extra columns to announce from this model.

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

void QgsAttributeTableModel::setRequest(const QgsFeatureRequest& request)

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

Parameters
request The request to use to fill this table model.

In contrast to a filter, the request will constrain the data shown without the possibility to dynamically adjust it.

QString QgsAttributeTableModel::sortCacheExpression(unsigned long cacheIndex = 0) const

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

void QgsAttributeTableModel::swapRows(QgsFeatureId a, QgsFeatureId b)

Swaps two rows.

Parameters
a first row
b second row

void QgsAttributeTableModel::progress(int i, bool& cancel) signal

void QgsAttributeTableModel::fieldConditionalStyleChanged(const QString& fieldName) public slot

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

Parameters
fieldName name of field whose conditional style has changed

bool QgsAttributeTableModel::loadFeatureAtId(QgsFeatureId fid) const virtual private

Load feature fid into local cache (mFeat)

Parameters
fid feature id
Returns feature exists

void QgsAttributeTableModel::attributeValueChanged(QgsFeatureId fid, int idx, const QVariant& value) virtual private slot

Launched when attribute value has been changed.

Parameters
fid feature id
idx attribute index
value new value

void QgsAttributeTableModel::featureAdded(QgsFeatureId fid, bool resettingModel = false) virtual private slot

Launched when a feature has been added.

Parameters
fid feature id
resettingModel set to true if model is in the process of being reset and the normal begin/EndInsertRows calls should not be made

void QgsAttributeTableModel::featuresDeleted(const QgsFeatureIds& fids) virtual private slot

Launched when features have been deleted.

Parameters
fids feature ids