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
Field origin. |
Methods
Utility function to get list of attribute indexes |
|
Appends a field. |
|
Appends an expression field. |
|
Returns the field at particular index (must be in range 0..N-1). |
|
Removes all fields |
|
Returns number of items |
|
Returns if a field index is valid |
|
Extends with fields from another |
|
Returns the field at particular index (must be in range 0..N-1). |
|
Returns the field's origin (value from an enumeration). |
|
Returns the field's origin index (its meaning is specific to each type of origin). |
|
Returns an icon corresponding to a field index, based on the field's type and source |
|
Gets the field index from the field name. |
|
Gets the field index from the field name. |
|
Checks whether the container is empty |
|
Looks up field's index from the field name. |
|
Returns a list with field names |
|
Removes the field with the given index. |
|
Renames a name of field. |
|
Returns number of items |
|
Utility function to return a list of |
Static Methods
Returns an icon corresponding to a field type |
- class qgis.core.QgsFields[source]¶
Bases:
object
- __init__()
Constructor for an empty field container
- __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
.FieldOriginAdded in version 3.38.
Unknown
: The field origin has not been specifiedAvailable as
QgsFields.OriginUnknown
in older QGIS releases.Provider
: Field originates from the underlying data provider of the vector layerAvailable as
QgsFields.OriginProvider
in older QGIS releases.Join
: Field originates from a joined layerAvailable as
QgsFields.OriginJoin
in older QGIS releases.Edit
: Field has been temporarily added in editing modeAvailable as
QgsFields.OriginEdit
in older QGIS releases.Expression
: Field is calculated from an expressionAvailable 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 theorigin
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 joinQgis
.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:
field (QgsField)
originIndex (int)
- 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:
- 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:
- 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:
- 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:
- 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 joinQgis
.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 optionaltypeString
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 optionaltypeString
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.
- lookupField(self, fieldName: str | None) int [source]¶
Looks up field’s index from the field name. This method matches in the following order:
The exact field name taking case sensitivity into account
Looks for the field name by case insensitive comparison
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.
- 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)