Subgroup: other

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 Append a field.
appendExpressionField Append an expression field.
at Gets field at particular index (must be in range 0..N-1)
clear Remove all fields
count Returns number of items
exists Returns if a field index is valid
extend Extend 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 Check whether the container is empty
lookupField Look up field’s index from the field name.
names Returns a list with field names
remove Remove a field with the given index
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

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

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

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

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

Extend 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

Check whether the container is empty

lookupField(self, fieldName: str) → int

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

Remove a field with the given index

size(self) → int

Returns number of items

toList(self) → object

Utility function to return a list of QgsField instances