QgsFieldFormatter class

A field formatter helps to handle and display values for a field.

Contents

It allows for using a shared configuration with the editor widgets for representation of attribute values. Field kits normally have one single instance which is managed by the QgsFieldFormatterRegistry. Custom field formatters should be registered there and field formatters for use within code should normally be obtained from there.

This is an abstract base class and will always need to be subclassed.

Derived classes

class QgsDateTimeFieldFormatter
Field formatter for a date time field.
class QgsFallbackFieldFormatter
A default fallback field formatter in case no specialized field formatter is defined.
class QgsKeyValueFieldFormatter
Field formatter for a key value field.
class QgsListFieldFormatter
Field formatter for a list field.
class QgsRangeFieldFormatter
Field formatter for a range (double) field with precision and locale.
class QgsRelationReferenceFieldFormatter
Field formatter for a relation reference field.
class QgsValueMapFieldFormatter
Field formatter for a ValueMap field.
class QgsValueRelationFieldFormatter
Field formatter for a value relation field.

Public functions

auto alignmentFlag(QgsVectorLayer* layer, int fieldIndex, const QVariantMap& config) const -> Qt::AlignmentFlag virtual
Returns the alignment for a particular field.
auto createCache(QgsVectorLayer* layer, int fieldIndex, const QVariantMap& config) const -> QVariant virtual
Create a cache for a given field.
auto id() const -> QString pure virtual
Returns a unique id for this field formatter.
auto representValue(QgsVectorLayer* layer, int fieldIndex, const QVariantMap& config, const QVariant& cache, const QVariant& value) const -> QString virtual
Create a pretty String representation of the value.
auto sortValue(QgsVectorLayer* layer, int fieldIndex, const QVariantMap& config, const QVariant& cache, const QVariant& value) const -> QVariant virtual
If the default sort order should be overwritten for this widget, you can transform the value in here.

Function documentation

Qt::AlignmentFlag QgsFieldFormatter::alignmentFlag(QgsVectorLayer* layer, int fieldIndex, const QVariantMap& config) const virtual

Returns the alignment for a particular field.

By default this will consider the field type but can be overwritten if mapped values are represented.

QVariant QgsFieldFormatter::createCache(QgsVectorLayer* layer, int fieldIndex, const QVariantMap& config) const virtual

Create a cache for a given field.

This will be used in situations where a field is being represented various times in a loop. And will be passed to other methods on QgsFieldKit and QgsEditorWidgetWrapper.

For example, the attribute table will create a cache once for each field and then use this cache for representation. The QgsValueRelationFieldFormatter and QgsValueRelationEditorWidget implement this functionality to create a lookuptable once (a QVariantMap / dict) and are make use of a cache if present.

QString QgsFieldFormatter::id() const pure virtual

Returns a unique id for this field formatter.

This id will later be used to identify this field formatter in the registry with QgsFieldFormatterRegistry::fieldFormatter().

This id matches the id of a QgsEditorWidgetFactory.

QString QgsFieldFormatter::representValue(QgsVectorLayer* layer, int fieldIndex, const QVariantMap& config, const QVariant& cache, const QVariant& value) const virtual

Create a pretty String representation of the value.

Returns By default the string representation of the provided value as implied by the field definition is returned.

QVariant QgsFieldFormatter::sortValue(QgsVectorLayer* layer, int fieldIndex, const QVariantMap& config, const QVariant& cache, const QVariant& value) const virtual

If the default sort order should be overwritten for this widget, you can transform the value in here.

Returns an unmodified value by default.