Class: QgsFieldModel

A model which displays 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.

Class Hierarchy

Inheritance diagram of qgis.core.QgsFieldModel

Base classes

QAbstractItemModel

QObject

class qgis.core.QgsFieldModel[source]

Bases: QAbstractItemModel

__init__(parent: QObject | None = None)

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

Parameters:

parent (Optional[QObject] = None)

class CustomRole(*values)

Bases: IntEnum

Custom model roles.

Note

Prior to QGIS 3.36 this was available as QgsFieldModel.FieldRoles

Added in version 3.36.

  • FieldName: Return field name if index corresponds to a field

    Available as QgsFieldModel.FieldNameRole in older QGIS releases.

  • FieldIndex: Return field index if index corresponds to a field

    Available as QgsFieldModel.FieldIndexRole in older QGIS releases.

  • Expression: Return field name or expression

    Available as QgsFieldModel.ExpressionRole in older QGIS releases.

  • IsExpression: Return if index corresponds to an expression

    Available as QgsFieldModel.IsExpressionRole in older QGIS releases.

  • ExpressionValidity: Return if expression is valid or not

    Available as QgsFieldModel.ExpressionValidityRole in older QGIS releases.

  • FieldType: Return the field type (if a field, return QVariant if expression)

    Available as QgsFieldModel.FieldTypeRole in older QGIS releases.

  • FieldOrigin: Return the field origin (if a field, returns QVariant if expression)

    Available as QgsFieldModel.FieldOriginRole in older QGIS releases.

  • IsEmpty: Return if the index corresponds to the empty value

    Available as QgsFieldModel.IsEmptyRole in older QGIS releases.

  • EditorWidgetType: Editor widget type

  • JoinedFieldIsEditable: True if a joined field is editable (returns QVariant if not a joined field)

  • FieldIsWidgetEditable: True if a is editable from the widget

FieldRoles

alias of CustomRole

allowEmptyFieldName(self) bool[source]

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

Return type:

bool

allowExpression(self) bool[source]

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

Return type:

bool

static fieldToolTip(field: QgsField) str[source]

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

Parameters:

field (QgsField)

Return type:

str

static fieldToolTipExtended(field: QgsField, layer: QgsVectorLayer | None) str[source]

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

Added in version 3.14.

Parameters:
Return type:

str

fields(self) QgsFields[source]

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

Added in version 3.14.

Return type:

QgsFields

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

Returns the index corresponding to a given fieldName.

Parameters:

fieldName (Optional[str])

Return type:

QModelIndex

isField(self, expression: str | None) bool[source]

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 (Optional[str])

Return type:

bool

layer(self) QgsVectorLayer | None[source]

Returns the layer associated with the model.

See also

setLayer()

Return type:

Optional[QgsVectorLayer]

removeExpression(self)[source]

Removes any custom expression from the model.

See also

setExpression()

setAllowEmptyFieldName(self, allowEmpty: bool)[source]

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

Parameters:

allowEmpty (bool)

setAllowExpression(self, allowExpression: bool)[source]

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

See also

setExpression()

Parameters:

allowExpression (bool)

setExpression(self, expression: str | None)[source]

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

Parameters:

expression (Optional[str])

setFields(self, fields: QgsFields)[source]

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

Added in version 3.14.

Parameters:

fields (QgsFields)

setLayer(self, layer: QgsVectorLayer | None)[source]

Set the layer from which fields are displayed.

See also

layer()

Parameters:

layer (Optional[QgsVectorLayer])

virtual updateModel(self)[source]

Called when the model must be updated.