Class: QgsFieldExpressionWidget

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

Bases: PyQt5.QtWidgets.QWidget

QgsFieldExpressionWidget creates a widget with a combo box to display the fields and expression and a button to open the expression dialog

The QgsFieldExpressionWidget class reates a widget to choose fields and edit expressions It contains a combo boxto display the fields and expression and a button to open the expression dialog. The combo box is editable, allowing expressions to be edited inline. The validity of the expression is checked live on key press, invalid expressions are displayed in red. The expression will be added to the model (and the fieldChanged signals emitted) only when editing in the line edit is finished (focus lost, enter key pressed).

Parameters

parent

actionEvent(self, QActionEvent)
allowEmptyFieldName(self) → bool

Returns True if the combo box allows the empty field (“not set”) choice.

New in version 3.4.6.

Return type

bool

allowEvalErrors(self) → bool

Allow accepting expressions with evaluation errors. This can be useful when we are not able to provide an expression context of which we are sure it’s completely populated.

New in version 3.0.

Return type

bool

allowEvalErrorsChanged

Allow accepting expressions with evaluation errors. This can be useful when we are not able to provide an expression context of which we are sure it’s completely populated.

New in version 3.0: [signal]

appendScope(self, scope: QgsExpressionContextScope)

Appends a scope to the current expression context.

Parameters

scope (QgsExpressionContextScope) – The scope to add.

New in version 3.2.

asExpression(self) → str

Returns the currently selected field or expression. If a field is currently selected, the returned value will be converted to a valid expression referencing this field (ie enclosing the field name with appropriate quotations).

New in version 2.14.

Return type

str

changeEvent(self, event: QEvent)
Parameters

event (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)
currentField(self) → Tuple[str, bool, bool]

currentField returns the currently selected field or expression if allowed

Parameters
  • isExpression – determines if the string returned is the name of a field or an expression

  • isValid – determines if the expression (or field) returned is valid

Return type

Tuple[str, bool, bool]

currentFieldChanged(self)
currentText(self) → str

Returns the current text that is set in the expression area

Return type

str

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)
editExpression(self)

open the expression dialog to edit the current or add a new expression

enterEvent(self, QEvent)
event(self, QEvent) → bool
eventFilter(self, watched: QObject, event: QEvent) → bool
Parameters
  • watched (QObject) –

  • event (QEvent) –

Return type

bool

expression(self) → str

Returns the currently selected field or expression. If a field is currently selected, the returned value will be converted to a valid expression referencing this field (ie enclosing the field name with appropriate quotations).

Alias for asExpression()

New in version 3.0.

Return type

str

expressionDialogTitle(self) → str

Returns the title used for the expression dialog

Return type

str

expressionEdited(self, expression: str)

when expression is edited by the user in the line edit, it will be checked for validity

Parameters

expression (str) –

expressionEditingFinished(self)

when expression has been edited (finished) it will be added to the model

fieldChanged(self, fieldName: str, isValid: bool)

fieldChanged signal with indication of the validity of the expression [signal]

Parameters

fieldName (str) –

filters(self) → QgsFieldProxyModel.Filters

currently used filter on list of fields

Return type

QgsFieldProxyModel.Filters

focusInEvent(self, QFocusEvent)
focusNextChild(self) → bool
focusNextPrevChild(self, bool) → bool
focusOutEvent(self, QFocusEvent)
focusPreviousChild(self) → bool
hideEvent(self, QHideEvent)
initPainter(self, QPainter)
inputMethodEvent(self, QInputMethodEvent)
isExpression(self) → bool

If the content is not just a simple field this method will return True.

Return type

bool

isExpressionValid(self, expressionStr: str) → bool
Parameters

expressionStr (str) –

Return type

bool

isSignalConnected(self, QMetaMethod) → bool
isValidExpression(self, expressionError: str = '') → bool

Returns True if the current expression is valid

Parameters

expressionError (str = '') –

Return type

bool

keyPressEvent(self, QKeyEvent)
keyReleaseEvent(self, QKeyEvent)
layer(self)QgsVectorLayer

Returns the layer currently associated with the widget.

See also

setLayer()

Return type

QgsVectorLayer

leaveEvent(self, QEvent)
metric(self, QPaintDevice.PaintDeviceMetric) → int
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]
paintEvent(self, QPaintEvent)
receivers(self, PYQT_SIGNAL) → int
registerExpressionContextGenerator(self, generator: QgsExpressionContextGenerator)

Register an expression context generator class that will be used to retrieve an expression context for the widget.

Parameters

generator (QgsExpressionContextGenerator) – A QgsExpressionContextGenerator class that will be used to create an expression context when required.

New in version 3.0.

resizeEvent(self, QResizeEvent)
sender(self) → QObject
senderSignalIndex(self) → int
setAllowEmptyFieldName(self, allowEmpty: bool)

Sets whether an optional empty field (“not set”) option is shown in the combo box.

New in version 3.4.6.

Parameters

allowEmpty (bool) –

setAllowEvalErrors(self, allowEvalErrors: bool)

Allow accepting expressions with evaluation errors. This can be useful when we are not able to provide an expression context of which we are sure it’s completely populated.

New in version 3.0.

Parameters

allowEvalErrors (bool) –

setExpression(self, expression: str)

Sets the current expression text and if applicable also the field. Alias for setField.

New in version 3.0.

Parameters

expression (str) –

setExpressionDialogTitle(self, title: str)

define the title used in the expression dialog

Parameters

title (str) –

setField(self, fieldName: str)

sets the current field or expression in the widget

Parameters

fieldName (str) –

setFilters(self, filters: Union[QgsFieldProxyModel.Filters, QgsFieldProxyModel.Filter])

setFilters allows fitering according to the type of field

Parameters

filters (Union[QgsFieldProxyModel.Filters) –

setGeomCalculator(self, da: QgsDistanceArea)

Sets the geometry calculator used in the expression dialog

Parameters

da (QgsDistanceArea) –

setLayer(self, layer: QgsMapLayer)

Sets the layer used to display the fields and expression.

See also

layer()

Parameters

layer (QgsMapLayer) –

setLeftHandButtonStyle(self, isLeft: bool)
Parameters

isLeft (bool) –

setRow(self, row: int)

sets the current row in the widget

Parameters

row (int) –

sharedPainter(self) → QPainter
showEvent(self, QShowEvent)
tabletEvent(self, QTabletEvent)
timerEvent(self, QTimerEvent)
updateLineEditStyle(self, expression: str = '')

updateLineEditStyle will re-style (color/font) the line edit depending on content and status

Parameters

expression (str = '') – if expression is given it will be evaluated for the given string, otherwise it takes current expression from the model

updateMicroFocus(self)
wheelEvent(self, QWheelEvent)