Class: QgsFieldFormatter

class qgis.core.QgsFieldFormatter

Bases: sip.wrapper

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

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.

New in version 3.0: Enums

Methods

alignmentFlag Returns the alignment for a particular field.
createCache Create a cache for a given field.
id Returns a unique id for this field formatter.
representValue Create a pretty String representation of the value.
sortValue If the default sort order should be overwritten for this widget, you can transform the value in here.

Signals

Attributes

alignmentFlag(self, layer: QgsVectorLayer, fieldIndex: int, config: Dict[str, Any]) → Qt.AlignmentFlag

Returns the alignment for a particular field. By default this will consider the field type but can be overwritten if mapped values are represented.

New in version 3.0.

createCache(self, layer: QgsVectorLayer, fieldIndex: int, config: Dict[str, Any]) → Any

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.

New in version 3.0.

id(self) → str

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.

representValue(self, layer: QgsVectorLayer, fieldIndex: int, config: Dict[str, Any], cache: Any, value: Any) → str

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.

New in version 3.0.

sortValue(self, layer: QgsVectorLayer, fieldIndex: int, config: Dict[str, Any], cache: Any, value: Any) → Any

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

Returns:an unmodified value by default.

New in version 3.0.