Class: QgsExpressionTreeView

A tree view to list all expressions functions, variables and fields that can be used in an expression.

Added in version 3.14.

Class Hierarchy

Inheritance diagram of qgis.gui.QgsExpressionTreeView

Base classes

QTreeView

QAbstractItemView

QAbstractScrollArea

QFrame

QWidget

QObject

QPaintDevice

Methods

currentItem

Returns the current item or a None

exportUserExpressions

Create the expressions JSON document storing all the user expressions to be exported.

expressionContext

Returns the expression context for the widget.

findExpressions

Returns the list of expression items matching a label.

loadExpressionsFromJson

Load and permanently store the expressions from the expressions JSON document.

loadFieldNames

This allows loading fields without specifying a layer

loadRecent

Loads the recent expressions from the given collection.

loadUserExpressions

Loads the user expressions.

project

Returns the project currently associated with the widget.

refresh

Refreshes the content of the tree

removeFromUserExpressions

Removes the expression label from the user stored expressions.

saveToRecent

Adds the current expression to the given collection.

saveToUserExpressions

Stores the user expression with given label and helpText.

setExpressionContext

Sets the expression context for the tree view.

setLayer

Sets layer in order to get the fields and values

setMenuProvider

Sets the menu provider.

setProject

Sets the project currently associated with the widget.

setSearchText

Sets the text to filter the expression tree

Signals

currentExpressionItemChanged

Emitter when the current expression item changed

expressionItemDoubleClicked

Emitted when a expression item is double clicked

class qgis.gui.QgsExpressionTreeView[source]

Bases: QTreeView

__init__(parent: QWidget | None = None)

Constructor

Parameters:

parent (Optional[QWidget] = None)

class MenuProvider[source]

Bases: object

Implementation of this interface can be implemented to allow QgsExpressionTreeView instance to provide custom context menus (opened upon right-click).

Added in version 3.14.

virtual createContextMenu(self, item: QgsExpressionItem | None) QMenu | None[source]

Returns a newly created menu instance

Parameters:

item (Optional[QgsExpressionItem])

Return type:

Optional[QMenu]

signal currentExpressionItemChanged(item: QgsExpressionItem)[source]

Emitter when the current expression item changed

Parameters:

item (QgsExpressionItem)

currentItem(self) QgsExpressionItem | None[source]

Returns the current item or a None

Return type:

Optional[QgsExpressionItem]

exportUserExpressions(self) QJsonDocument[source]

Create the expressions JSON document storing all the user expressions to be exported.

Return type:

QJsonDocument

Returns:

the created expressions JSON file

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 expressionItemDoubleClicked(text: str)[source]

Emitted when a expression item is double clicked

Parameters:

text (str)

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

Returns the list of expression items matching a label.

Parameters:

label (Optional[str])

Return type:

List[QgsExpressionItem]

loadExpressionsFromJson(self, expressionsDocument: QJsonDocument)[source]

Load and permanently store the expressions from the expressions JSON document.

Parameters:

expressionsDocument (QJsonDocument) – the parsed expressions JSON file

loadFieldNames(self, fields: QgsFields)[source]

This allows loading fields without specifying a layer

Parameters:

fields (QgsFields)

loadRecent(self, collection: str | None = '')[source]

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

Parameters:

collection (Optional[str] = '')

loadUserExpressions(self)[source]

Loads the user expressions. This is done on request since it can be very slow if there are thousands of user expressions

project(self) QgsProject | None[source]

Returns the project currently associated with the widget.

See also

setProject()

Return type:

Optional[QgsProject]

refresh(self)[source]

Refreshes the content of the tree

removeFromUserExpressions(self, label: str | None)[source]

Removes the expression label from the user stored expressions.

Parameters:

label (Optional[str])

saveToRecent(self, expressionText: str | None, collection: str | None = '')[source]

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

Parameters:
  • expressionText (Optional[str])

  • collection (Optional[str] = '')

saveToUserExpressions(self, label: str | None, expression: str | None, helpText: str | None)[source]

Stores the user expression with given label and helpText.

Parameters:
  • label (Optional[str])

  • expression (Optional[str])

  • helpText (Optional[str])

setExpressionContext(self, context: QgsExpressionContext)[source]

Sets the expression context for the tree view. The context is used to populate the list of available functions and variables.

Parameters:

context (QgsExpressionContext) – expression context

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

Sets layer in order to get the fields and values

Parameters:

layer (Optional[QgsVectorLayer])

setMenuProvider(self, provider: QgsExpressionTreeView.MenuProvider | None)[source]

Sets the menu provider. This does not take ownership of the provider

Parameters:

provider (Optional[QgsExpressionTreeView.MenuProvider])

setProject(self, project: QgsProject | None)[source]

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

Parameters:

project (Optional[QgsProject])

setSearchText(self, text: str | None)[source]

Sets the text to filter the expression tree

Parameters:

text (Optional[str])