QGIS API Documentation 3.41.0-Master (3c143d501a8)
Loading...
Searching...
No Matches
Public Slots | Signals | Public Member Functions | Protected Slots | Protected Member Functions | Properties | Friends | List of all members
QgsFieldExpressionWidget Class Reference

The QgsFieldExpressionWidget class creates a widget to choose fields and edit expressions It contains a combo box to display the fields and expression and a button to open the expression dialog. More...

#include <qgsfieldexpressionwidget.h>

Inheritance diagram for QgsFieldExpressionWidget:
Inheritance graph
[legend]

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 setFields (const QgsFields &fields)
 Sets the fields used in the widget to fields, this allows the widget to work without a layer.
 
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
 

Signals

void allowEvalErrorsChanged ()
 Allow accepting expressions with evaluation errors.
 
void buttonVisibleChanged ()
 Emitted when the button visibility changes.
 
void fieldChanged (const QString &fieldName)
 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 Member Functions

 QgsFieldExpressionWidget (QWidget *parent=nullptr)
 QgsFieldExpressionWidget creates a widget with a combo box to display the fields and expression and a button to open the expression dialog.
 
bool allowEmptyFieldName () const
 Returns true if the combo box allows the empty field ("not set") choice.
 
bool allowEvalErrors () const
 Allow accepting expressions with evaluation errors.
 
void appendScope (QgsExpressionContextScope *scope)
 Appends a scope to the current expression context.
 
QString asExpression () const
 Returns the currently selected field or expression.
 
bool buttonVisible () const
 Returns the visibility of the button.
 
QString currentField (bool *isExpression=nullptr, bool *isValid=nullptr) const
 currentField returns the currently selected field or expression if allowed
 
QString currentText () const
 Returns the current text that is set in the expression area.
 
QString expression () const
 Returns the currently selected field or expression.
 
const QString expressionDialogTitle ()
 Returns the title used for the expression dialog.
 
QgsFieldProxyModel::Filters filters () const
 currently used filter on list of fields
 
bool isExpression () const
 If the content is not just a simple field this method will return true.
 
bool isValidExpression (QString *expressionError=nullptr) const
 Returns true if the current expression is valid.
 
QgsVectorLayerlayer () const
 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 setAllowEmptyFieldName (bool allowEmpty)
 Sets whether an optional empty field ("not set") option is shown in the combo box.
 
void setAllowEvalErrors (bool allowEvalErrors)
 Allow accepting expressions with evaluation errors.
 
void setButtonVisible (bool visible)
 Set the visibility of the button.
 
void setCustomPreviewGenerator (const QString &label, const QList< QPair< QString, QVariant > > &choices, const std::function< QgsExpressionContext(const QVariant &) > &previewContextGenerator)
 Sets the widget to run using a custom preview generator.
 
void setExpressionDialogTitle (const QString &title)
 define the title used in the expression dialog
 
void setFilters (QgsFieldProxyModel::Filters filters)
 setFilters allows filtering according to the type of field
 
void setGeomCalculator (const QgsDistanceArea &da)
 Sets the geometry calculator used in the expression dialog.
 
void setLeftHandButtonStyle (bool isLeft)
 

Protected Slots

void currentFieldChanged ()
 
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
 
bool isExpressionValid (const QString &expressionStr)
 
void updateLineEditStyle (const QString &expression=QString())
 updateLineEditStyle will re-style (color/font) the line edit depending on content and status
 

Protected Member Functions

void changeEvent (QEvent *event) override
 
bool eventFilter (QObject *watched, QEvent *event) override
 

Properties

bool allowEmptyFieldName
 
bool allowEvalErrors
 
bool buttonVisible
 
QString expressionDialogTitle
 
QgsFieldProxyModel::Filters filters
 

Friends

class TestQgsFieldExpressionWidget
 

Detailed Description

The QgsFieldExpressionWidget class creates a widget to choose fields and edit expressions It contains a combo box to 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).

Definition at line 46 of file qgsfieldexpressionwidget.h.

Constructor & Destructor Documentation

◆ QgsFieldExpressionWidget()

QgsFieldExpressionWidget::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.

Definition at line 33 of file qgsfieldexpressionwidget.cpp.

Member Function Documentation

◆ allowEmptyFieldName()

bool QgsFieldExpressionWidget::allowEmptyFieldName ( ) const

Returns true if the combo box allows the empty field ("not set") choice.

See also
setAllowEmptyFieldName()
Since
QGIS 3.4.6

Definition at line 89 of file qgsfieldexpressionwidget.cpp.

◆ allowEvalErrors()

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.

Definition at line 324 of file qgsfieldexpressionwidget.cpp.

◆ allowEvalErrorsChanged

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.

◆ appendScope()

void QgsFieldExpressionWidget::appendScope ( QgsExpressionContextScope scope)

Appends a scope to the current expression context.

Parameters
scopeThe scope to add.
Since
QGIS 3.2

Definition at line 414 of file qgsfieldexpressionwidget.cpp.

◆ asExpression()

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

Definition at line 119 of file qgsfieldexpressionwidget.cpp.

◆ buttonVisible()

bool QgsFieldExpressionWidget::buttonVisible ( ) const

Returns the visibility of the button.

If button is hidden, the widget essentially becomes an editable combo box

Since
QGIS 3.36

Definition at line 339 of file qgsfieldexpressionwidget.cpp.

◆ buttonVisibleChanged

void QgsFieldExpressionWidget::buttonVisibleChanged ( )
signal

Emitted when the button visibility changes.

Since
QGIS 3.36

◆ changeEvent()

void QgsFieldExpressionWidget::changeEvent ( QEvent *  event)
overrideprotected

Definition at line 285 of file qgsfieldexpressionwidget.cpp.

◆ currentField()

QString QgsFieldExpressionWidget::currentField ( bool *  isExpression = nullptr,
bool *  isValid = nullptr 
) const

currentField returns the currently selected field or expression if allowed

Parameters
isExpressiondetermines if the string returned is the name of a field or an expression
isValiddetermines if the expression (or field) returned is valid

Definition at line 140 of file qgsfieldexpressionwidget.cpp.

◆ currentFieldChanged

void QgsFieldExpressionWidget::currentFieldChanged ( )
protectedslot

Definition at line 353 of file qgsfieldexpressionwidget.cpp.

◆ currentText()

QString QgsFieldExpressionWidget::currentText ( ) const

Returns the current text that is set in the expression area.

Definition at line 114 of file qgsfieldexpressionwidget.cpp.

◆ editExpression

void QgsFieldExpressionWidget::editExpression ( )
protectedslot

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

Definition at line 239 of file qgsfieldexpressionwidget.cpp.

◆ eventFilter()

bool QgsFieldExpressionWidget::eventFilter ( QObject *  watched,
QEvent *  event 
)
overrideprotected

Definition at line 310 of file qgsfieldexpressionwidget.cpp.

◆ expression()

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

Definition at line 124 of file qgsfieldexpressionwidget.cpp.

◆ expressionDialogTitle()

const QString QgsFieldExpressionWidget::expressionDialogTitle ( )
inline

Returns the title used for the expression dialog.

Definition at line 75 of file qgsfieldexpressionwidget.h.

◆ expressionEdited

void QgsFieldExpressionWidget::expressionEdited ( const QString &  expression)
protectedslot

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

Definition at line 269 of file qgsfieldexpressionwidget.cpp.

◆ expressionEditingFinished

void QgsFieldExpressionWidget::expressionEditingFinished ( )
protectedslot

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

Definition at line 275 of file qgsfieldexpressionwidget.cpp.

◆ fieldChanged [1/2]

void QgsFieldExpressionWidget::fieldChanged ( const QString &  fieldName)
signal

Emitted when the currently selected field changes.

◆ fieldChanged [2/2]

void QgsFieldExpressionWidget::fieldChanged ( const QString &  fieldName,
bool  isValid 
)
signal

fieldChanged signal with indication of the validity of the expression

◆ filters()

QgsFieldProxyModel::Filters QgsFieldExpressionWidget::filters ( ) const
inline

currently used filter on list of fields

Definition at line 97 of file qgsfieldexpressionwidget.h.

◆ isExpression()

bool QgsFieldExpressionWidget::isExpression ( ) const

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

Definition at line 135 of file qgsfieldexpressionwidget.cpp.

◆ isExpressionValid

bool QgsFieldExpressionWidget::isExpressionValid ( const QString &  expressionStr)
protectedslot

Definition at line 407 of file qgsfieldexpressionwidget.cpp.

◆ isValidExpression()

bool QgsFieldExpressionWidget::isValidExpression ( QString *  expressionError = nullptr) const

Returns true if the current expression is valid.

Definition at line 129 of file qgsfieldexpressionwidget.cpp.

◆ layer()

QgsVectorLayer * QgsFieldExpressionWidget::layer ( ) const

Returns the layer currently associated with the widget.

See also
setLayer()

Definition at line 156 of file qgsfieldexpressionwidget.cpp.

◆ registerExpressionContextGenerator()

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
generatorA QgsExpressionContextGenerator class that will be used to create an expression context when required.

Definition at line 161 of file qgsfieldexpressionwidget.cpp.

◆ setAllowEmptyFieldName()

void QgsFieldExpressionWidget::setAllowEmptyFieldName ( bool  allowEmpty)

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

See also
allowEmptyFieldName()
Since
QGIS 3.4.6

Definition at line 83 of file qgsfieldexpressionwidget.cpp.

◆ setAllowEvalErrors()

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.

Definition at line 329 of file qgsfieldexpressionwidget.cpp.

◆ setButtonVisible()

void QgsFieldExpressionWidget::setButtonVisible ( bool  visible)

Set the visibility of the button.

If button is hidden, the widget essentially becomes an editable combo box

Since
QGIS 3.36

Definition at line 344 of file qgsfieldexpressionwidget.cpp.

◆ setCustomPreviewGenerator()

void QgsFieldExpressionWidget::setCustomPreviewGenerator ( const QString &  label,
const QList< QPair< QString, QVariant > > &  choices,
const std::function< QgsExpressionContext(const QVariant &) > &  previewContextGenerator 
)

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
labelThe 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
choicesA 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.
previewContextGeneratorA function which takes a QVariant representing the object to preview, and returns a QgsExpressionContext to use for previewing the object.
Since
QGIS 3.38

Definition at line 166 of file qgsfieldexpressionwidget.cpp.

◆ setExpression

void QgsFieldExpressionWidget::setExpression ( const QString &  expression)
slot

Sets the current expression text and if applicable also the field.

Alias for setField.

Definition at line 234 of file qgsfieldexpressionwidget.cpp.

◆ setExpressionDialogTitle()

void QgsFieldExpressionWidget::setExpressionDialogTitle ( const QString &  title)

define the title used in the expression dialog

Definition at line 73 of file qgsfieldexpressionwidget.cpp.

◆ setField

void QgsFieldExpressionWidget::setField ( const QString &  fieldName)
slot

sets the current field or expression in the widget

Definition at line 191 of file qgsfieldexpressionwidget.cpp.

◆ setFields

void QgsFieldExpressionWidget::setFields ( const QgsFields fields)
slot

Sets the fields used in the widget to fields, this allows the widget to work without a layer.

Since
QGIS 3.14

Definition at line 229 of file qgsfieldexpressionwidget.cpp.

◆ setFilters()

void QgsFieldExpressionWidget::setFilters ( QgsFieldProxyModel::Filters  filters)

setFilters allows filtering according to the type of field

Definition at line 78 of file qgsfieldexpressionwidget.cpp.

◆ setGeomCalculator()

void QgsFieldExpressionWidget::setGeomCalculator ( const QgsDistanceArea da)

Sets the geometry calculator used in the expression dialog.

Definition at line 109 of file qgsfieldexpressionwidget.cpp.

◆ setLayer

void QgsFieldExpressionWidget::setLayer ( QgsMapLayer layer)
slot

Sets the layer used to display the fields and expression.

See also
layer()

Definition at line 173 of file qgsfieldexpressionwidget.cpp.

◆ setLeftHandButtonStyle()

void QgsFieldExpressionWidget::setLeftHandButtonStyle ( bool  isLeft)

Definition at line 94 of file qgsfieldexpressionwidget.cpp.

◆ setRow

void QgsFieldExpressionWidget::setRow ( int  row)
inlineslot

sets the current row in the widget

Definition at line 273 of file qgsfieldexpressionwidget.h.

◆ updateLineEditStyle

void QgsFieldExpressionWidget::updateLineEditStyle ( const QString &  expression = QString())
protectedslot

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

Parameters
expressionif expression is given it will be evaluated for the given string, otherwise it takes current expression from the model

Definition at line 375 of file qgsfieldexpressionwidget.cpp.

Friends And Related Symbol Documentation

◆ TestQgsFieldExpressionWidget

friend class TestQgsFieldExpressionWidget
friend

Definition at line 338 of file qgsfieldexpressionwidget.h.

Property Documentation

◆ allowEmptyFieldName

bool QgsFieldExpressionWidget::allowEmptyFieldName
readwrite

Definition at line 51 of file qgsfieldexpressionwidget.h.

◆ allowEvalErrors

bool QgsFieldExpressionWidget::allowEvalErrors
readwrite

Definition at line 52 of file qgsfieldexpressionwidget.h.

◆ buttonVisible

bool QgsFieldExpressionWidget::buttonVisible
readwrite

Definition at line 53 of file qgsfieldexpressionwidget.h.

◆ expressionDialogTitle

QString QgsFieldExpressionWidget::expressionDialogTitle
readwrite

Definition at line 49 of file qgsfieldexpressionwidget.h.

◆ filters

QgsFieldProxyModel::Filters QgsFieldExpressionWidget::filters
readwrite

Definition at line 50 of file qgsfieldexpressionwidget.h.


The documentation for this class was generated from the following files: