Getting my expression to process the text in field

I found a solution worked for me.

  • Add this script to the "Function Editor":
from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def isSelected(feat, field, feature, parent):
    layer = QgsProject.instance().mapLayersByName(field)[0]
    for f in layer.getSelectedFeatures():
        if f['fid'] == feat['fid']:
            return 1 # True
    return 0 # False
  • Run this expression for layer A (see the picture below):
isSelected(
  get_feature('C', 'fid', "fid"),
  lyr_field
)

Explanation:

  • get_feature('C', 'fid', "fid"):

    Return the feature of C matching (fid in C == "fid" in A)

  • QgsProject.instance().mapLayersByName(field)[0]:

    Return the layer matching the lyr_field value (B here) of the feature in A.

  • layer.getSelectedFeatures():

    Get selected features in B.

  • if f['fid'] == feat['fid']:

    If fid value of one of the selected features in B exists in C then return 1, else return 0

If get_feature('C', 'fid', "fid") returns None, the expression returns nothing.

I used aa field to populate.

enter image description here


See below the changed code: you will not need the parameter "entidad" you can use the default parameter "feature" instead:

from qgis.core import *
from qgis.gui import *
from qgis.utils import iface

@qgsfunction(args='auto', group='Custom')
def isSelected(capa, feature, parent):
    proy= QgsProject.instance()
    capap= proy.mapLayersByName(capa)[0]
    les= list(capap.getSelectedFeatures())
    boolean=False
    for f in les:
        if f.id() == feature.id():
            boolean= True
            break
        else:
            continue
    return boolean