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.

Enums

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

Signals

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.

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)

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

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

at(self, i: int) → QgsField

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

clear(self)

Removes all fields

count(self) → int

Returns number of items

exists(self, i: int) → bool

Returns if a field index is valid

Parameters:i – Index of the field which needs to be checked
Returns:True if the field exists
extend(self, other: QgsFields)

Extends with fields from another QgsFields container

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

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

Gets field’s origin (value from an enumeration)

fieldOriginIndex(self, fieldIdx: int) → int

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

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.

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 – The name of the field.
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 – The name of the field.
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

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 – The name to look for.
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.

remove(self, fieldIdx: int)

Removes a field with the given index

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.

size(self) → int

Returns number of items

toList(self) → object

Utility function to return a list of QgsField instances