Class: QgsExpressionBuilderWidget

class qgis.gui.QgsExpressionBuilderWidget(parent: QWidget = None)

Bases: PyQt5.QtWidgets.QWidget

Create a new expression builder widget with an optional parent.

A reusable widget that can be used to build a expression string. See QgsExpressionBuilderDialog for example of usage.

Parameters

parent

class Flag

Bases: int

baseClass

alias of QgsExpressionBuilderWidget

class Flags

Bases: sip.wrapper

QgsExpressionBuilderWidget.Flags(Union[QgsExpressionBuilderWidget.Flags, QgsExpressionBuilderWidget.Flag]) QgsExpressionBuilderWidget.Flags(QgsExpressionBuilderWidget.Flags)

LoadAll = 6
LoadNothing = 0
LoadRecent = 2
LoadUserExpressions = 4
actionEvent(self, QActionEvent)
autosave(self)

Auto save the current Python function code.

changeEvent(self, QEvent)
childEvent(self, QChildEvent)
closeEvent(self, QCloseEvent)
connectNotify(self, QMetaMethod)
contextMenuEvent(self, QContextMenuEvent)
create(self, window: sip.voidptr = 0, initializeWindow: bool = True, destroyOldWindow: bool = True)
customEvent(self, QEvent)
destroy(self, destroyWindow: bool = True, destroySubWindows: bool = True)
disconnectNotify(self, QMetaMethod)
dragEnterEvent(self, QDragEnterEvent)
dragLeaveEvent(self, QDragLeaveEvent)
dragMoveEvent(self, QDragMoveEvent)
dropEvent(self, QDropEvent)
editSelectedUserExpression(self)

Edits the selected expression from the stored user expressions, the selected expression must be a user stored expression.

New in version 3.14.

enterEvent(self, QEvent)
evalError(self) → bool

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

New in version 3.0.

Return type

bool

evalErrorChanged

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

New in version 3.0: [signal]

event(self, QEvent) → bool
expectedOutputFormat(self) → str

The set expected format string. This is pure text format and no expression validation is done against it.

Return type

str

Returns

The expected value format.

expressionContext(self)QgsExpressionContext

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.

New in version 2.12.

Return type

QgsExpressionContext

expressionParsed

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. [signal]

expressionText(self) → str

Gets the expression string that has been set in the expression area.

Return type

str

Returns

The expression as a string.

expressionTree(self)QgsExpressionTreeView

Returns the expression tree

New in version 3.14.

Return type

QgsExpressionTreeView

findExpressions(self, label: str) → List[QgsExpressionItem]

Returns the list of expression items matching a label.

New in version 3.12.

Deprecated since version QGIS: 3.14 use expressionTree()->findExpressions instead

Parameters

label (str) –

Return type

List[QgsExpressionItem]

focusInEvent(self, QFocusEvent)
focusNextChild(self) → bool
focusNextPrevChild(self, bool) → bool
focusOutEvent(self, QFocusEvent)
focusPreviousChild(self) → bool
hideEvent(self, QHideEvent)
init(self, context: QgsExpressionContext = QgsExpressionContext(), recentCollection: str = '', flags: Union[QgsExpressionBuilderWidget.Flags, QgsExpressionBuilderWidget.Flag] = QgsExpressionBuilderWidget.LoadAll)

Initialize without any layer

New in version 3.14.

Parameters
initPainter(self, QPainter)
initWithFields(self, fields: QgsFields, context: QgsExpressionContext = QgsExpressionContext(), recentCollection: str = '', flags: Union[QgsExpressionBuilderWidget.Flags, QgsExpressionBuilderWidget.Flag] = QgsExpressionBuilderWidget.LoadAll)

Initialize with given fields without any layer

New in version 3.14.

Parameters
initWithLayer(self, layer: QgsVectorLayer, context: QgsExpressionContext = QgsExpressionContext(), recentCollection: str = '', flags: Union[QgsExpressionBuilderWidget.Flags, QgsExpressionBuilderWidget.Flag] = QgsExpressionBuilderWidget.LoadAll)

Initialize with a layer

New in version 3.14.

Parameters
inputMethodEvent(self, QInputMethodEvent)
isExpressionValid(self) → bool

Returns if the expression is valid

Return type

bool

isSignalConnected(self, QMetaMethod) → bool
keyPressEvent(self, QKeyEvent)
keyReleaseEvent(self, QKeyEvent)
layer(self)QgsVectorLayer

Returns the current layer or a None.

Return type

QgsVectorLayer

leaveEvent(self, QEvent)
loadAllUsedValues(self)

Load all unique values from the set layer into the sample area. Only the used ones. Without available values, even if the formatter can provide them (eg. RelationReference).

New in version 3.12.

loadAllValues(self)

Load all unique values from the set layer into the sample area. Including all available values, in case the formatter can provide them (eg. RelationReference).

loadCodeFromFile(self, path: str)

Loads code from the given file into the function editor

Parameters

path (str) –

loadFieldNames(self)

Deprecated since version QGIS: 3.14 this is now done automatically

loadFieldNames(self, fields: QgsFields)

Deprecated since version QGIS: 3.14 use expressionTree()->loadFieldNames() instead

loadFieldsAndValues(self, fieldValues: Dict[str, Iterable[str]])

Loads field names and values from the specified map.

New in version 2.12.

Deprecated since version QGIS: 3.14 this will not do anything, use setLayer() instead

Parameters

fieldValues (Dict[str) –

loadFunctionCode(self, code: str)

Loads code into the function editor

Parameters

code (str) –

loadRecent(self, collection: str = '')

Loads the recent expressions from the given collection. By default it is loaded from the collection “generic”.

Deprecated since version QGIS: 3.14 use expressionTree()->loadRecent() instead

Parameters

collection (str = '') –

loadSampleUsedValues(self)

Load used sample values into the sample value area. Only the used ones. Without available values, even if the formatter can provide them (eg. RelationReference).

New in version 3.12.

loadSampleValues(self)

Load sample values into the sample value area. Including available values, in case the formatter can provide them (eg. RelationReference).

loadUserExpressions(self)

Loads the user expressions.

Deprecated since version QGIS: 3.14 use expressionTree()->loadUserExpressions() instead

New in version 3.12.

metric(self, QPaintDevice.PaintDeviceMetric) → int
model(self) → QStandardItemModel

Returns a pointer to the dialog’s function item model. This method is exposed for testing purposes only - it should not be used to modify the model.

New in version 3.0.

Deprecated since version QGIS: 3.14

Return type

QStandardItemModel

mouseDoubleClickEvent(self, QMouseEvent)
mouseMoveEvent(self, QMouseEvent)
mousePressEvent(self, QMouseEvent)
mouseReleaseEvent(self, QMouseEvent)
moveEvent(self, QMoveEvent)
nativeEvent(self, Union[QByteArray, bytes, bytearray], sip.voidptr) → Tuple[bool, int]
newFunctionFile(self, fileName: str = '')

Creates a new file in the function editor

Parameters

fileName (str = '') –

paintEvent(self, QPaintEvent)
parserError(self) → bool

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

New in version 3.0.

Return type

bool

parserErrorChanged

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

New in version 3.0: [signal]

project(self)QgsProject

Returns the project currently associated with the widget.

See also

setProject()

New in version 3.0.

Return type

QgsProject

receivers(self, PYQT_SIGNAL) → int
removeFromUserExpressions(self, label: str)

Removes the expression label from the user stored expressions.

Deprecated since version QGIS: 3.14 use expressionTree()->removeFromUserExpressions() instead

New in version 3.12.

Parameters

label (str) –

removeSelectedUserExpression(self)

Removes the selected expression from the stored user expressions, the selected expression must be a user stored expression.

New in version 3.12.

resizeEvent(self, QResizeEvent)
saveFunctionFile(self, fileName: str)

Saves the current function editor text to the given file.

Parameters

fileName (str) –

saveToRecent(self, collection: str = '')

Adds the current expression to the given collection. By default it is saved to the collection “generic”.

Deprecated since version QGIS: 3.14 use expressionTree()->saveRecent() instead

Parameters

collection (str = '') –

saveToUserExpressions(self, label: str, expression: str, helpText: str)

Stores the user expression with given label and helpText.

Deprecated since version QGIS: 3.14 use expressionTree()->saveToUserExpressions() instead

New in version 3.12.

Parameters
  • label (str) –

  • expression (str) –

  • helpText (str) –

sender(self) → QObject
senderSignalIndex(self) → int
setAutoSave(self, enabled: bool)

Enabled or disable auto saving. When enabled Python scripts will be auto saved when text changes.

Parameters

enabled (bool) – True to enable auto saving.

setExpectedOutputFormat(self, expected: str)

The set expected format string. This is pure text format and no expression validation is done against it.

Parameters

expected (str) – The expected value format for the expression.

Note

Only a UI hint and not used for expression validation.

setExpressionContext(self, context: QgsExpressionContext)

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

New in version 2.12.

setExpressionText(self, expression: str)

Sets the expression string for the widget

Parameters

expression (str) –

setGeomCalculator(self, da: QgsDistanceArea)

Sets geometry calculator used in distance/area calculations.

Parameters

da (QgsDistanceArea) –

setLayer(self, layer: QgsVectorLayer)

Sets layer in order to get the fields and values

Note

this needs to be called before calling loadFieldNames().

Parameters

layer (QgsVectorLayer) –

setProject(self, project: QgsProject)

Sets the project currently associated with the widget. This controls which layers and relations and other project-specific items are shown in the widget.

See also

project()

New in version 3.0.

Parameters

project (QgsProject) –

sharedPainter(self) → QPainter
showEvent(self, e: QShowEvent)
Parameters

e (QShowEvent) –

storeCurrentUserExpression(self)

Adds the current expressions to the stored user expressions.

New in version 3.12.

tabletEvent(self, QTabletEvent)
timerEvent(self, QTimerEvent)
updateFunctionFileList(self, path: str)

Updates the list of function files found at the given path

Parameters

path (str) –

updateMicroFocus(self)
wheelEvent(self, QWheelEvent)