QgsFields class

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)

Public types

enum FieldOrigin { OriginUnknown, OriginProvider, OriginJoin, OriginEdit, OriginExpression }

Constructors, destructors, conversion operators

QgsFields()
Constructor for an empty field container.
QgsFields(const QgsFields& other)
Copy constructor.
operator QVariant() const
Allows direct construction of QVariants from fields.

Public functions

auto allAttributesList() const -> QgsAttributeList
Utility function to get list of attribute indexes.
auto append(const QgsField& field, FieldOrigin origin = OriginProvider, int originIndex = -1) -> bool
Append a field. The field must have unique name, otherwise it is rejected (returns false)
auto appendExpressionField(const QgsField& field, int originIndex) -> bool
Append an expression field. The field must have unique name, otherwise it is rejected (returns false)
auto at(int i) const -> QgsField
Gets field at particular index (must be in range 0..N-1)
auto begin() const -> const_iterator noexcept
Returns a const STL-style iterator pointing to the first item in the list.
auto begin() -> iterator
Returns an STL-style iterator pointing to the first item in the list.
void clear()
Remove all fields.
auto constBegin() const -> const_iterator noexcept
Returns a const STL-style iterator pointing to the first item in the list.
auto constEnd() const -> const_iterator noexcept
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list.
auto count() const -> int
Returns number of items.
auto end() const -> const_iterator noexcept
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list.
auto end() -> iterator
Returns an STL-style iterator pointing to the imaginary item after the last item in the list.
auto exists(int i) const -> bool
Returns if a field index is valid.
void extend(const QgsFields& other)
Extend with fields from another QgsFields container.
auto field(int fieldIdx) const -> QgsField
Gets field at particular index (must be in range 0..N-1)
auto field(const QString& name) const -> QgsField
Gets field with matching name.
auto fieldOrigin(int fieldIdx) const -> FieldOrigin
Gets field's origin (value from an enumeration)
auto fieldOriginIndex(int fieldIdx) const -> int
Gets field's origin index (its meaning is specific to each type of origin)
auto iconForField(int fieldIdx) const -> QIcon
Returns an icon corresponding to a field index, based on the field's type and source.
auto indexFromName(const QString& fieldName) const -> int
Gets the field index from the field name.
auto indexOf(const QString& fieldName) const -> int
Gets the field index from the field name.
auto isEmpty() const -> bool
Check whether the container is empty.
auto lookupField(const QString& fieldName) const -> int
Look up field's index from the field name.
auto names() const -> QStringList
Returns a list with field names.
auto operator!=(const QgsFields& other) const -> bool
auto operator=(const QgsFields& other) -> QgsFields&
Assignment operator.
auto operator==(const QgsFields& other) const -> bool
auto operator[](int i) const -> QgsField
Gets field at particular index (must be in range 0..N-1)
auto operator[](int i) -> QgsField&
Gets field at particular index (must be in range 0..N-1)
void remove(int fieldIdx)
Remove a field with the given index.
auto size() const -> int
Returns number of items.
auto toList() const -> QList<QgsField>
Utility function to return a list of QgsField instances.

Enum documentation

enum QgsFields::FieldOrigin

Enumerators
OriginUnknown

It has not been specified where the field comes from.

OriginProvider

Field comes from the underlying data provider of the vector layer (originIndex = index in provider's fields)

OriginJoin

Field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index within the join)

OriginEdit

Field has been temporarily added in editing mode (originIndex = index in the list of added attributes)

OriginExpression

Field is calculated from an expression.

Function documentation

QgsAttributeList QgsFields::allAttributesList() const

Utility function to get list of attribute indexes.

const_iterator QgsFields::begin() const noexcept

Returns a const STL-style iterator pointing to the first item in the list.

iterator QgsFields::begin()

Returns an STL-style iterator pointing to the first item in the list.

const_iterator QgsFields::constBegin() const noexcept

Returns a const STL-style iterator pointing to the first item in the list.

const_iterator QgsFields::constEnd() const noexcept

Returns a const STL-style iterator pointing to the imaginary item after the last item in the list.

const_iterator QgsFields::end() const noexcept

Returns a const STL-style iterator pointing to the imaginary item after the last item in the list.

iterator QgsFields::end()

Returns an STL-style iterator pointing to the imaginary item after the last item in the list.

bool QgsFields::exists(int i) const

Returns if a field index is valid.

Parameters
i Index of the field which needs to be checked
Returns True if the field exists

QIcon QgsFields::iconForField(int fieldIdx) const

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

int QgsFields::indexFromName(const QString& fieldName) const

Gets the field index from the field name.

Parameters
fieldName The name of the field.
Returns The field index if found or -1 in case it cannot be found.

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

int QgsFields::indexOf(const QString& fieldName) const

Gets the field index from the field name.

Parameters
fieldName The name of the field.
Returns The field index if found or -1 in case it cannot be found.

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

int QgsFields::lookupField(const QString& fieldName) const

Look up field's index from the field name.

Parameters
fieldName The name to look for.
Returns The field index if found or -1 in case it cannot be found.

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)

QStringList QgsFields::names() const

Returns a list with field names.

bool QgsFields::operator!=(const QgsFields& other) const

bool QgsFields::operator==(const QgsFields& other) const