Class: QgsFields

Container of fields for a vector layer.

In addition to storing a list of QgsField instances, it also:

  • allows quick lookups of field names to index in the list

  • keeps track of where the field definition comes from (vector data provider, joined layer or newly added from an editing operation)

Note

QgsFields objects are implicitly shared.

Enums

FieldOrigin

Field origin.

Methods

allAttributesList

Utility function to get list of attribute indexes

append

Appends a field.

appendExpressionField

Appends an expression field.

at

Returns the field at particular index (must be in range 0..N-1).

clear

Removes all fields

count

Returns number of items

exists

Returns if a field index is valid

extend

Extends with fields from another QgsFields container

field

Returns the field at particular index (must be in range 0..N-1).

fieldOrigin

Returns the field's origin (value from an enumeration).

fieldOriginIndex

Returns the field's origin index (its meaning is specific to each type of origin).

iconForField

Returns an icon corresponding to a field index, based on the field's type and source

indexFromName

Gets the field index from the field name.

indexOf

Gets the field index from the field name.

isEmpty

Checks whether the container is empty

lookupField

Looks up field's index from the field name.

names

Returns a list with field names

remove

Removes the field with the given index.

rename

Renames a name of field.

size

Returns number of items

toList

Utility function to return a list of QgsField instances

Static Methods

iconForFieldType

Returns an icon corresponding to a field type

class qgis.core.QgsFields[source]

Bases: object

__init__()

Constructor for an empty field container

__init__(other: QgsFields)
Parameters:

other (QgsFields)

__init__(fields: Any)

Construct QgsFields from a list of fields.

Added in version 3.40.

Parameters:

fields (Any)

class FieldOrigin(*values)

Bases: IntEnum

Field origin.

Note

Prior to QGIS 3.38 this was available as QgsFields.FieldOrigin

Added in version 3.38.

  • Unknown: The field origin has not been specified

    Available as QgsFields.OriginUnknown in older QGIS releases.

  • Provider: Field originates from the underlying data provider of the vector layer

    Available as QgsFields.OriginProvider in older QGIS releases.

  • Join: Field originates from a joined layer

    Available as QgsFields.OriginJoin in older QGIS releases.

  • Edit: Field has been temporarily added in editing mode

    Available as QgsFields.OriginEdit in older QGIS releases.

  • Expression: Field is calculated from an expression

    Available as QgsFields.OriginExpression in older QGIS releases.

allAttributesList(self) List[int][source]

Utility function to get list of attribute indexes

Return type:

List[int]

append(self, field: QgsField, origin: Qgis.FieldOrigin = Qgis.FieldOrigin.Provider, originIndex: int = -1) bool[source]

Appends a field.

The field must have a unique name, otherwise it is rejected (returns False).

The originIndex argument must be set to a value corresponding to the origin type:

  • Qgis.FieldOrigin.Provider: The field’s originIndex is the index in provider’s fields.

  • Qgis.FieldOrigin.Join: The field’s originIndex / 1000 = index of the join, originIndex % 1000 = index within the join

  • Qgis.FieldOrigin.Edit: The originIndex is the index in the list of added attributes

Parameters:
  • field (QgsField)

  • origin (Qgis.FieldOrigin = Qgis.FieldOrigin.Provider)

  • originIndex (int = -1)

Return type:

bool

append(self, fields: Any, origin: Qgis.FieldOrigin = Qgis.FieldOrigin.Provider) bool[source]

Appends a list of fields.

The fields must have unique names, otherwise it is rejected (returns False).

Added in version 3.40.

Parameters:
  • fields (Any)

  • origin (Qgis.FieldOrigin = Qgis.FieldOrigin.Provider)

Return type:

bool

append(self, fields: QgsFields) bool[source]

Appends another set of fields to these fields.

The fields must have unique names, otherwise it is rejected (returns False).

Added in version 3.40.

Parameters:

fields (QgsFields)

Return type:

bool

appendExpressionField(self, field: QgsField, originIndex: int) bool[source]

Appends an expression field. The field must have unique name, otherwise it is rejected (returns False)

Parameters:
Return type:

bool

at(self, i: int) QgsField[source]

Returns the field at particular index (must be in range 0..N-1).

Raises:

KeyError – if no field exists at the specified index

Parameters:

i (int)

Return type:

QgsField

clear(self)[source]

Removes all fields

count(self) int[source]

Returns number of items

Return type:

int

exists(self, i: int) bool[source]

Returns if a field index is valid

Parameters:

i (int) – Index of the field which needs to be checked

Return type:

bool

Returns:

True if the field exists

extend(self, other: QgsFields)[source]

Extends with fields from another QgsFields container

Parameters:

other (QgsFields)

field(self, fieldIdx: int) QgsField[source]

Returns the field at particular index (must be in range 0..N-1).

Raises:

KeyError – if no field exists at the specified index

Parameters:

fieldIdx (int)

Return type:

QgsField

field(self, name: str | None) QgsField[source]

Returns the field with matching name.

Raises:

KeyError – if no matching field was found.

Parameters:

name (Optional[str])

Return type:

QgsField

fieldOrigin(self, fieldIdx: int) Qgis.FieldOrigin[source]

Returns the field’s origin (value from an enumeration).

Raises:

KeyError – if no field exists at the specified index

Parameters:

fieldIdx (int)

Return type:

Qgis.FieldOrigin

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

Returns the field’s origin index (its meaning is specific to each type of origin).

  • Qgis.FieldOrigin.Provider: The field’s originIndex is the index in provider’s fields.

  • Qgis.FieldOrigin.Join: The field’s originIndex / 1000 = index of the join, originIndex % 1000 = index within the join

  • Qgis.FieldOrigin.Edit: The originIndex is the index in the list of added attributes

Raises:

KeyError – if no field exists at the specified index

Parameters:

fieldIdx (int)

Return type:

int

iconForField(self, fieldIdx: int, considerOrigin: bool = False) QIcon[source]

Returns an icon corresponding to a field index, based on the field’s type and source

Parameters:
  • fieldIdx (int) – the field index

  • considerOrigin (bool = False) – if True the icon will the origin of the field

Raises:

KeyError – if no field exists at the specified index

Return type:

QIcon

static iconForFieldType(type: QMetaType.Type, subType: QMetaType.Type = QMetaType.Type.UnknownType, typeString: str | None = '') QIcon[source]

Returns an icon corresponding to a field type

Since QGIS 3.24, the optional subType argument can be used to specify the type of variant list or map values. Since QGIS 3.30, the optional typeString argument can be used to specify field type strings for handling user field types.

Added in version 3.16.

Parameters:
  • type (QMetaType.Type)

  • subType (QMetaType.Type = QMetaType.Type.UnknownType)

  • typeString (Optional[str] = '')

Return type:

QIcon

static iconForFieldType(type: QVariant.Type, subType: QVariant.Type, typeString: str | None = '') QIcon[source]

Returns an icon corresponding to a field type

Since QGIS 3.24, the optional subType argument can be used to specify the type of variant list or map values. Since QGIS 3.30, the optional typeString argument can be used to specify field type strings for handling user field types.

Added in version 3.16.

Deprecated since version 3.38: Use the method with a QMetaType.Type argument instead.

Parameters:
  • type (QVariant.Type)

  • subType (QVariant.Type)

  • typeString (Optional[str] = '')

Return type:

QIcon

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

Gets the field index from the field name.

This method is case sensitive and only matches the data source name of the field.

Alias for indexOf

Parameters:

fieldName (Optional[str]) – The name of the field.

Return type:

int

Returns:

The field index if found or -1 in case it cannot be found.

See also

lookupField() For a more tolerant alternative.

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

Gets the field index from the field name.

This method is case sensitive and only matches the data source name of the field.

Parameters:

fieldName (Optional[str]) – The name of the field.

Return type:

int

Returns:

The field index if found or -1 in case it cannot be found.

See also

lookupField() For a more tolerant alternative.

isEmpty(self) bool[source]

Checks whether the container is empty

Return type:

bool

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

Looks up field’s index from the field name. This method matches in the following order:

  1. The exact field name taking case sensitivity into account

  2. Looks for the field name by case insensitive comparison

  3. The field alias (case insensitive)

Parameters:

fieldName (Optional[str]) – The name to look for.

Return type:

int

Returns:

The field index if found or -1 in case it cannot be found.

See also

indexFromName() For a more performant and precise but less tolerant alternative.

names(self) List[str][source]

Returns a list with field names

Return type:

List[str]

remove(self, fieldIdx: int)[source]

Removes the field with the given index.

Raises:

KeyError – if no field with the specified index exists

Parameters:

fieldIdx (int)

rename(self, fieldIdx: int, name: str | None) bool[source]

Renames a name of field. The field must have unique name, otherwise change is rejected (returns False)

Added in version 3.6.

Parameters:
  • fieldIdx (int)

  • name (Optional[str])

Return type:

bool

size(self) int[source]

Returns number of items

Return type:

int

toList(self) Any

Utility function to return a list of QgsField instances

Return type:

Any