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.

QgsFieldFormatter() Default constructor

QgsFieldFormatter(QgsFieldFormatter)

Methods

alignmentFlag

Returns the alignment for a particular field.

availableValues

Returns a list of the values that would be possible to select with this widget type On a RelationReference that would be the parents ids or on ValueMap all the configured keys according to the settings in the config

createCache

Create a cache for a given field.

flags

Returns the flags

id

Returns a unique id for this field formatter.

representValue

Create a pretty String representation of the value.

setFlags

Sets the flags

sortValue

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

Attributes

CanProvideAvailableValues

CanProvideAvailableValues = 1
class Flag

Bases: int

class Flags
class Flags(Union[QgsFieldFormatter.Flags, QgsFieldFormatter.Flag])
class Flags(QgsFieldFormatter.Flags)

Bases: sip.wrapper

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.

Parameters:
  • layer (QgsVectorLayer) –

  • fieldIndex (int) –

  • config (Dict[str) –

Return type:

Qt.AlignmentFlag

availableValues(self, config: Dict[str, Any], countLimit: int, context: QgsFieldFormatterContext) List[Any]

Returns a list of the values that would be possible to select with this widget type On a RelationReference that would be the parents ids or on ValueMap all the configured keys according to the settings in the config

New in version 3.12.

Parameters:
Return type:

List[Any]

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.

Parameters:
  • layer (QgsVectorLayer) –

  • fieldIndex (int) –

  • config (Dict[str) –

Return type:

Any

flags(self) QgsFieldFormatter.Flags

Returns the flags

New in version 3.12.

Return type:

QgsFieldFormatter.Flags

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.

Return type:

str

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

Create a pretty String representation of the value.

Return type:

str

Returns:

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

New in version 3.0.

Parameters:
  • layer (QgsVectorLayer) –

  • fieldIndex (int) –

  • config (Dict[str) –

  • cache (Any) –

  • value (Any) –

setFlags(self, flags: QgsFieldFormatter.Flags | QgsFieldFormatter.Flag)

Sets the flags

New in version 3.12.

Parameters:

flags (Union[QgsFieldFormatter.Flags) –

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.

Return type:

Any

Returns:

an unmodified value by default.

New in version 3.0.

Parameters:
  • layer (QgsVectorLayer) –

  • fieldIndex (int) –

  • config (Dict[str) –

  • cache (Any) –

  • value (Any) –