2

I just tried creating the following function on the function editor on the select by expression

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

@qgsfunction(args=0, group='Python')
def is_selected(values,feature,parent):
layer = qgis.utils.iface.activeLayer()
return feature.id() in layer.selectedFeaturesIds()

However, even if I press the load button, when I go back to the custom , it doesn't list it.

How to add my function?

  • Sorry if this is obvious, but did you click on [New file] button before entering your code into the editor? – Kazuhito Sep 12 '17 at 12:15
  • 1
    @Kazuhito yes, I now have 3 different scripts on the left side but only the default scratch one appears – Luffydude Sep 12 '17 at 13:15
4

Your syntax in last line isn't good... You need to use list comprehension. Try with something like below, it will return a string which is a list of the selected features's id.

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

@qgsfunction(args=0, group='Python')
def is_selected(values,feature,parent):
    layer = qgis.utils.iface.activeLayer()
    return ', '.join([str(id) for id in layer.selectedFeaturesIds()])

You have to click on the Load button to add the function in your expressions.

After that, your function will appears in the Python group because of the argument (group='Python') of your decorator.

  • Tried you syntax but it still doesn't appear under custom after loading – Luffydude Sep 12 '17 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.