QgsFieldExpressionWidget class
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.
Contents
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).
Constructors, destructors, conversion operators
- QgsFieldExpressionWidget(QWidget* parent = nullptr) explicit
- QgsFieldExpressionWidget creates a widget with a combo box to display the fields and expression and a button to open the expression dialog.
Public functions
- auto allowEvalErrors() const -> bool
- Allow accepting expressions with evaluation errors.
- void appendScope(QgsExpressionContextScope* scope)
- Appends a scope to the current expression context.
- auto asExpression() const -> QString
- Returns the currently selected field or expression.
- auto currentField(bool* isExpression = nullptr, bool* isValid = nullptr) const -> QString
- currentField returns the currently selected field or expression if allowed
- auto currentText() const -> QString
- Returns the current text that is set in the expression area.
- auto expression() const -> QString
- Returns the currently selected field or expression.
- auto expressionDialogTitle() -> const QString
- Returns the title used for the expression dialog.
- auto filters() const -> QgsFieldProxyModel::Filters
- currently used filter on list of fields
- auto isExpression() const -> bool
- If the content is not just a simple field this method will return true.
- auto isValidExpression(QString* expressionError = nullptr) const -> bool
- Returns true if the current expression is valid.
- auto layer() const -> QgsVectorLayer*
- Returns the layer currently associated with the widget.
- void registerExpressionContextGenerator(const QgsExpressionContextGenerator* generator)
- Register an expression context generator class that will be used to retrieve an expression context for the widget.
- void setAllowEvalErrors(bool allowEvalErrors)
- Allow accepting expressions with evaluation errors.
- void setExpressionDialogTitle(const QString& title)
- define the title used in the expression dialog
- void setFilters(QgsFieldProxyModel::Filters filters)
- setFilters allows fitering according to the type of field
- void setGeomCalculator(const QgsDistanceArea& da)
- Sets the geometry calculator used in the expression dialog.
Signals
- void allowEvalErrorsChanged()
- Allow accepting expressions with evaluation errors.
- void fieldChanged(const QString& fieldName)
- the signal is emitted when the currently selected field changes
- void fieldChanged(const QString& fieldName, bool isValid)
- fieldChanged signal with indication of the validity of the expression
Public slots
- void setExpression(const QString& expression)
- Sets the current expression text and if applicable also the field.
- void setField(const QString& fieldName)
- sets the current field or expression in the widget
- void setLayer(QgsMapLayer* layer)
- Sets the layer used to display the fields and expression.
- void setRow(int row)
- sets the current row in the widget
Protected slots
- void editExpression()
- open the expression dialog to edit the current or add a new expression
- void expressionEdited(const QString& expression)
- when expression is edited by the user in the line edit, it will be checked for validity
- void expressionEditingFinished()
- when expression has been edited (finished) it will be added to the model
- void updateLineEditStyle(const QString& expression = QString())
- updateLineEditStyle will re-style (color/font) the line edit depending on content and status
Function documentation
bool QgsFieldExpressionWidget:: allowEvalErrors() const
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.
void QgsFieldExpressionWidget:: appendScope(QgsExpressionContextScope* scope)
Appends a scope to the current expression context.
| Parameters | |
|---|---|
| scope | The scope to add. |
QString QgsFieldExpressionWidget:: asExpression() const
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).
QString QgsFieldExpressionWidget:: currentField(bool* isExpression = nullptr,
bool* isValid = nullptr) const
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 |
QString QgsFieldExpressionWidget:: expression() const
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()
QgsVectorLayer* QgsFieldExpressionWidget:: layer() const
Returns the layer currently associated with the widget.
void QgsFieldExpressionWidget:: registerExpressionContextGenerator(const QgsExpressionContextGenerator* generator)
Register an expression context generator class that will be used to retrieve an expression context for the widget.
| Parameters | |
|---|---|
| generator | A QgsExpressionContextGenerator class that will be used to create an expression context when required. |
void QgsFieldExpressionWidget:: setAllowEvalErrors(bool allowEvalErrors)
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.
void QgsFieldExpressionWidget:: allowEvalErrorsChanged() signal
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.
void QgsFieldExpressionWidget:: setExpression(const QString& expression) public slot
Sets the current expression text and if applicable also the field.
Alias for setField.
void QgsFieldExpressionWidget:: setLayer(QgsMapLayer* layer) public slot
Sets the layer used to display the fields and expression.
void QgsFieldExpressionWidget:: updateLineEditStyle(const QString& expression = QString()) protected slot
updateLineEditStyle will re-style (color/font) the line edit depending on content and status
| Parameters | |
|---|---|
| expression | if expression is given it will be evaluated for the given string, otherwise it takes current expression from the model |