Class: QgsFields

class qgis.core.QgsFields

Bases: sip.wrapper

Constructor for an empty field container

QgsFields(other: QgsFields) Copy constructor

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.

Methods

allAttributesList

Utility function to get list of attribute indexes

append

Appends a field.

appendExpressionField

Appends an expression field.

at

Gets 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

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

fieldOrigin

Gets field’s origin (value from an enumeration)

fieldOriginIndex

Gets 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 a 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

Attributes

OriginEdit

OriginExpression

OriginJoin

OriginProvider

OriginUnknown

class FieldOrigin

Bases: int

OriginEdit = 3
OriginExpression = 4
OriginJoin = 2
OriginProvider = 1
OriginUnknown = 0
allAttributesList(self) → List[int]

Utility function to get list of attribute indexes

New in version 2.4.

Return type

List[int]

append(self, field: QgsField, origin: QgsFields.FieldOrigin = QgsFields.OriginProvider, originIndex: int = -1) → bool

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

Parameters
  • field (QgsField) –

  • origin (QgsFields.FieldOrigin = QgsFields.OriginProvider) –

  • originIndex (int = -1) –

Return type

bool

appendExpressionField(self, field: QgsField, originIndex: int) → bool

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

Parameters
  • field (QgsField) –

  • originIndex (int) –

Return type

bool

at(self, i: int) → QgsField

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

Parameters

i (int) –

Return type

QgsField

clear(self)

Removes all fields

count(self) → int

Returns number of items

Return type

int

exists(self, i: int) → bool

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)

Extends with fields from another QgsFields container

Parameters

other (QgsFields) –

field(self, fieldIdx: int) → QgsField

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

field(self, name: str) -> QgsField Gets field with matching name

Parameters

fieldIdx (int) –

Return type

QgsField

fieldOrigin(self, fieldIdx: int) → QgsFields.FieldOrigin

Gets field’s origin (value from an enumeration)

Parameters

fieldIdx (int) –

Return type

QgsFields.FieldOrigin

fieldOriginIndex(self, fieldIdx: int) → int

Gets field’s origin index (its meaning is specific to each type of origin)

Parameters

fieldIdx (int) –

Return type

int

iconForField(self, fieldIdx: int) → QIcon

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

New in version 2.14.

Parameters

fieldIdx (int) –

Return type

QIcon

indexFromName(self, fieldName: str) → int

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

indexOf(self, fieldName: str) → int

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

New in version 3.0.

isEmpty(self) → bool

Checks whether the container is empty

Return type

bool

lookupField(self, fieldName: str) → int

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

New in version 2.4.

names(self) → List[str]

Returns a list with field names

New in version 3.0.

Return type

List[str]

remove(self, fieldIdx: int)

Removes a field with the given index

Parameters

fieldIdx (int) –

rename(self, fieldIdx: int, name: str) → bool

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

New in version 3.6.

Parameters
  • fieldIdx (int) –

  • name (str) –

Return type

bool

size(self) → int

Returns number of items

Return type

int

toList(self) → object

Utility function to return a list of QgsField instances

Return type

object