Bug report #17893

Variables don't work in filter of value relation widget

Added by Daan Goedkoop about 6 years ago. Updated about 6 years ago.

Status:Closed
Priority:Normal
Assignee:-
Category:Edit widget
Affected QGIS version:master Regression?:No
Operating System: Easy fix?:Yes
Pull Request or Patch supplied:No Resolution:fixed/implemented
Crashes QGIS or corrupts data:No Copied to github as #:25788

Description

If e.g. a project variable (with the '@variable') notation is used in the filter field of a value relation widget, this doesn't work. While configuring it does work, but when closing the layer properties and actually using the widget, it doesn't.

I have looked into the source code, and I think the problem is (for MASTER) in src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp: no expression context is set. I think in the following block:

if ( !config.value( QStringLiteral( "FilterExpression" ) ).toString().isEmpty() )
{
request.setFilterExpression( config.value( QStringLiteral( "FilterExpression" ) ).toString() );
}

...it needs to be changed to something like this:

if ( !config.value( QStringLiteral( "FilterExpression" ) ).toString().isEmpty() )
{
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( layer ) );
request.setExpressionContext( context );
request.setFilterExpression( config.value( QStringLiteral( "FilterExpression" ) ).toString() );
}

(Btw. The problem is also present in QGIS 2.18.)

History

#1 Updated by Daan Goedkoop about 6 years ago

I have tested this with QGIS 2.18:

https://github.com/dgoedkoop/QGIS/commit/3e0c129bab20bb02b62afe53f6288dc5a33e873f

This does indeed fix the problem. (Apparently QGIS 2.18 didn't have the QgsExpressionContextUtils::globalProjectLayerScopes function yet.)

Btw. The filter of atlas objects in the print composer seems to have the same problem! Also both in Master and in 2.18.

#3 Updated by Daan Goedkoop about 6 years ago

  • Status changed from Open to Closed

#4 Updated by Giovanni Manghi about 6 years ago

  • Resolution set to fixed/implemented

Also available in: Atom PDF