Class: QgsFieldModel

class qgis.core.QgsFieldModel(parent: QObject = None)

Bases: PyQt5.QtCore.QAbstractItemModel

Constructor for QgsFieldModel - creates a model to display the fields of a given layer.

The QgsFieldModel class is a model to display the list of fields in widgets (optionally associated with a vector layer). If allowed, expressions might be added to the end of the model. It can be associated with a QgsMapLayerModel to dynamically display a layer and its fields.

New in version 2.3.

Parameters

parent

EditorWidgetType = 265
ExpressionRole = 259
ExpressionValidityRole = 261
FieldIndexRole = 258
FieldIsWidgetEditable = 267
FieldNameRole = 257
FieldOriginRole = 263
class FieldRoles

Bases: int

FieldTypeRole = 262
IsEmptyRole = 264
IsExpressionRole = 260
JoinedFieldIsEditable = 266
allowEmptyFieldName(self) → bool

Returns True if the model allows the empty field (“not set”) choice.

New in version 3.0.

Return type

bool

allowExpression(self) → bool

Returns True if the model allows custom expressions to be created and displayed.

Return type

bool

beginInsertColumns()
beginInsertRows()
beginMoveColumns()
beginMoveRows()
beginRemoveColumns()
beginRemoveRows()
beginResetModel()
changePersistentIndex()
changePersistentIndexList()
childEvent()
columnCount(self, parent: QModelIndex) → int
Parameters

parent (QModelIndex) –

Return type

int

connectNotify()
createIndex()
customEvent()
data(self, index: QModelIndex, role: int) → Any
Parameters
  • index (QModelIndex) –

  • role (int) –

Return type

Any

decodeData()
disconnectNotify()
encodeData()
endInsertColumns()
endInsertRows()
endMoveColumns()
endMoveRows()
endRemoveColumns()
endRemoveRows()
endResetModel()
fieldToolTip(field: QgsField) → str

Returns a HTML formatted tooltip string for a field, containing details like the field name, alias and type.

New in version 3.0.

Parameters

field (QgsField) –

Return type

str

fieldToolTipExtended(field: QgsField, layer: QgsVectorLayer) → str

Returns a HTML formatted tooltip string for a field, containing details like the field name, alias, type and expression.

New in version 3.14.

Parameters
Return type

str

fields(self)QgsFields

Returns the fields currently shown in the model.

This will either be fields from the associated layer() or the fields manually set by a call to setFields().

New in version 3.14.

Return type

QgsFields

index(self, row: int, column: int, parent: QModelIndex = QModelIndex()) → QModelIndex
Parameters
  • row (int) –

  • column (int) –

  • parent (QModelIndex = QModelIndex()) –

Return type

QModelIndex

indexFromName(self, fieldName: str) → QModelIndex

Returns the index corresponding to a given fieldName.

Parameters

fieldName (str) –

Return type

QModelIndex

isField(self, expression: str) → bool

Returns True if a string represents a field reference, or False if it is an expression consisting of more than direct field reference.

Parameters

expression (str) –

Return type

bool

isSignalConnected()
layer(self)QgsVectorLayer

Returns the layer associated with the model.

See also

setLayer()

Return type

QgsVectorLayer

parent(self, child: QModelIndex) → QModelIndex
Parameters

child (QModelIndex) –

Return type

QModelIndex

persistentIndexList()
receivers()
removeExpression(self)

Removes any custom expression from the model.

See also

setExpression()

resetInternalData()
rowCount(self, parent: QModelIndex = QModelIndex()) → int
Parameters

parent (QModelIndex = QModelIndex()) –

Return type

int

sender()
senderSignalIndex()
setAllowEmptyFieldName(self, allowEmpty: bool)

Sets whether an optional empty field (“not set”) option is present in the model.

New in version 3.0.

Parameters

allowEmpty (bool) –

setAllowExpression(self, allowExpression: bool)

Sets whether custom expressions are accepted and displayed in the model.

See also

setExpression()

Parameters

allowExpression (bool) –

setExpression(self, expression: str)

Sets a single expression to be added after the fields at the end of the model.

Parameters

expression (str) –

setFields(self, fields: QgsFields)

Manually sets the fields to use for the model.

This method should only be used when the model ISN’T associated with a layer() and needs to show the fields from an arbitrary field collection instead. Calling setFields() will automatically clear any existing layer().

See also

fields()

New in version 3.14.

Parameters

fields (QgsFields) –

setLayer(self, layer: QgsVectorLayer)

Set the layer from which fields are displayed.

See also

layer()

Parameters

layer (QgsVectorLayer) –

timerEvent()
updateModel(self)

Called when the model must be updated.