I am building an expression, whose function is to return True if a characteristic is selected.
The expression requires two arguments, the Feature and the name of the layer.
If I enter the name of the layer as text it works properly. But if I enter a field containing the name of the layer it does not work, even if the record contains exactly the name.
Here is the code of the expression:
from qgis.core import *
from qgis.gui import *
from qgis.utils import iface
@qgsfunction(args='auto', group='Custom')
def isSelected(entidad, capa, feature, parent):
proy= QgsProject.instance()
capap= proy.mapLayersByName(capa)[0]
les= list(capap.getSelectedFeatures())
boolean=False
for f in les:
if f.id() == entidad.id():
boolean= True
break
else:
continue
return boolean
Note:
- The equivalent function, exists (
is_selected
) but does not work properly in my QGIS, version 3.4 - The code works properly on the console
isSelected( $currentfeature, "<fieldname>" )
notation as well asisSelected( $currentfeature, attribute( '<fieldname>' ) )
, where the field<fieldname>
holds a string literal with a valid layer name. Are you certain your literals are not wrapped in any way? Did the internals formapLayersByName
change in any way between versions, removing or adding wrapping characters?