QgsFieldModel class

The QgsFieldModel class is a model to display the list of fields of a layer in widgets.

If allowed, expressions might be added to the end of the model. It can be associated with a QgsMapLayerModel to dynamically display a layer and its fields.

Public types

enum FieldRoles { FieldNameRole = Qt::UserRole + 1, FieldIndexRole = Qt::UserRole + 2, ExpressionRole = Qt::UserRole + 3, IsExpressionRole = Qt::UserRole + 4, ExpressionValidityRole = Qt::UserRole + 5, FieldTypeRole = Qt::UserRole + 6, FieldOriginRole = Qt::UserRole + 7, IsEmptyRole = Qt::UserRole + 8 }
Roles utilized by the model.

Public static functions

static auto fieldToolTip(const QgsField& field) -> QString
Returns a HTML formatted tooltip string for a field, containing details like the field name, alias and type.

Constructors, destructors, conversion operators

QgsFieldModel(QObject* parent = nullptr) explicit
Constructor for QgsFieldModel - creates a model to display the fields of a given layer.

Public functions

auto allowEmptyFieldName() const -> bool
Returns true if the model allows the empty field ("not set") choice.
auto allowExpression() -> bool
Returns true if the model allows custom expressions to be created and displayed.
auto indexFromName(const QString& fieldName) -> QModelIndex
Returns the index corresponding to a given fieldName.
auto isField(const QString& expression) const -> bool
Returns true if a string represents a field reference, or false if it is an expression consisting of more than direct field reference.
auto layer() -> QgsVectorLayer*
Returns the layer associated with the model.
void removeExpression()
Removes any custom expression from the model.
void setAllowEmptyFieldName(bool allowEmpty)
Sets whether an optional empty field ("not set") option is present in the model.
void setAllowExpression(bool allowExpression)
Sets whether custom expressions are accepted and displayed in the model.
void setExpression(const QString& expression)
Sets a single expression to be added after the fields at the end of the model.

Public slots

void setLayer(QgsVectorLayer* layer)
Set the layer from which fields are displayed.

Protected slots

void updateModel() virtual
Called when the model must be updated.

Enum documentation

enum QgsFieldModel::FieldRoles

Roles utilized by the model.

Enumerators
FieldNameRole

Return field name if index corresponds to a field.

FieldIndexRole

Return field index if index corresponds to a field.

ExpressionRole

Return field name or expression.

IsExpressionRole

Return if index corresponds to an expression.

ExpressionValidityRole

Return if expression is valid or not.

FieldTypeRole

Return the field type (if a field, return QVariant if expression)

FieldOriginRole

Return the field origin (if a field, returns QVariant if expression)

IsEmptyRole

Return if the index corresponds to the empty value.

Function documentation

static QString QgsFieldModel::fieldToolTip(const QgsField& field)

Returns a HTML formatted tooltip string for a field, containing details like the field name, alias and type.

bool QgsFieldModel::allowEmptyFieldName() const

Returns true if the model allows the empty field ("not set") choice.

bool QgsFieldModel::allowExpression()

Returns true if the model allows custom expressions to be created and displayed.

QgsVectorLayer* QgsFieldModel::layer()

Returns the layer associated with the model.

void QgsFieldModel::removeExpression()

Removes any custom expression from the model.

void QgsFieldModel::setAllowEmptyFieldName(bool allowEmpty)

Sets whether an optional empty field ("not set") option is present in the model.

void QgsFieldModel::setAllowExpression(bool allowExpression)

Sets whether custom expressions are accepted and displayed in the model.

void QgsFieldModel::setExpression(const QString& expression)

Sets a single expression to be added after the fields at the end of the model.

void QgsFieldModel::setLayer(QgsVectorLayer* layer) public slot

Set the layer from which fields are displayed.