Class: QgsExpressionPreviewWidget

A widget for previewing an expression result.

If the layer is set, one can browse across features to see the different outputs.

Added in version 3.14.

Class Hierarchy

Inheritance diagram of qgis.gui.QgsExpressionPreviewWidget

Base classes

QWidget

QObject

QPaintDevice

Methods

currentPreviewText

Returns the current expression result preview text.

evalError

Will be set to True if the current expression text reported an eval error with the context.

expressionContext

Returns the expression context for the widget.

parserError

Will be set to True if the current expression text reports a parser error with the context.

parserErrors

Returns the expression parser errors

rootNode

Returns the root node of the expression

setCurrentFeature

sets the current feature used

setCustomPreviewGenerator

Sets the widget to run using a custom preview generator.

setExpressionContext

Sets the expression context for the widget.

setExpressionText

Sets the expression

setGeomCalculator

Sets geometry calculator used in distance/area calculations.

setLayer

Sets the layer used in the preview

Signals

evalErrorChanged

Will be set to True if the current expression text reported an eval error with the context.

expressionParsed

Emitted when the user changes the expression in the widget.

parserErrorChanged

Will be set to True if the current expression text reported a parser error with the context.

toolTipChanged

Emitted whenever the tool tip changed

class qgis.gui.QgsExpressionPreviewWidget[source]

Bases: QWidget

__init__(parent: QWidget | None = None)

Constructor

Parameters:

parent (Optional[QWidget] = None)

currentPreviewText(self) str[source]

Returns the current expression result preview text.

Added in version 3.38.

Return type:

str

evalError(self) bool[source]

Will be set to True if the current expression text reported an eval error with the context.

Return type:

bool

signal evalErrorChanged[source]

Will be set to True if the current expression text reported an eval error with the context.

expressionContext(self) QgsExpressionContext[source]

Returns the expression context for the widget. The context is used for the expression preview result and for populating the list of available functions and variables.

Return type:

QgsExpressionContext

signal expressionParsed(isValid: bool)[source]

Emitted when the user changes the expression in the widget. Users of this widget should connect to this signal to decide if to let the user continue.

Parameters:

isValid (bool) – Is True if the expression the user has typed is valid.

parserError(self) bool[source]

Will be set to True if the current expression text reports a parser error with the context.

Return type:

bool

signal parserErrorChanged[source]

Will be set to True if the current expression text reported a parser error with the context.

parserErrors(self) List[QgsExpression.ParserError]

Returns the expression parser errors

Return type:

List[QgsExpression.ParserError]

rootNode(self) QgsExpressionNode | None[source]

Returns the root node of the expression

Return type:

Optional[QgsExpressionNode]

setCurrentFeature(self, feature: QgsFeature)[source]

sets the current feature used

Parameters:

feature (QgsFeature)

setCustomPreviewGenerator(self, label: str | None, choices: Iterable[Tuple[str | None, Any]], a2: Callable[..., None])[source]

Sets the widget to run using a custom preview generator.

In this mode, the widget will call a callback function to generate a new QgsExpressionContext as the previewed object changes. This can be used to provide custom preview values for different objects (i.e. for objects which aren’t vector layer features).

Parameters:
  • label (Optional[str]) – The label to display for the combo box presenting choices of objects. This should be a representative name, eg “Band” if the widget is showing choices of raster layer bands

  • choices (Iterable[Tuple[Optional[str], Any]]) – A list of choices to present to the user. Each choice is a pair of a human-readable label and a QVariant representing the object to preview.

  • previewContextGenerator – A function which takes a QVariant representing the object to preview, and returns a QgsExpressionContext to use for previewing the object.

Added in version 3.38.

Parameters:

a2 (Callable[..., None])

setExpressionContext(self, context: QgsExpressionContext)[source]

Sets the expression context for the widget. The context is used for the expression preview result and to populate the list of available functions and variables.

Parameters:

context (QgsExpressionContext) – expression context

setExpressionText(self, expression: str | None)[source]

Sets the expression

Parameters:

expression (Optional[str])

setGeomCalculator(self, da: QgsDistanceArea)[source]

Sets geometry calculator used in distance/area calculations.

Parameters:

da (QgsDistanceArea)

setLayer(self, layer: QgsVectorLayer | None)[source]

Sets the layer used in the preview

Parameters:

layer (Optional[QgsVectorLayer])

signal toolTipChanged(toolTip: str)[source]

Emitted whenever the tool tip changed

Parameters:

toolTip (str)