3

QGIS is such an amazing tool with lots of useful features, so that it's easy to overlook some of them.

Just to be sure it's not already implemented in QGIS 2.18:

Is there any Attribute Table option to run a layer Action on all currently selected features?

I'm using a Processing script as workaround, but it would be nice to have this feature directly implemented in the Attribute Table.

##MyTools=group
##VectorLayer=vector
##action_name=string
from qgis.core import *
import qgis.utils
from qgis.gui import *

layer = processing.getObject(VectorLayer)
actionManager = layer.actions()
actions = actionManager.listActions()
if len(actions) > 0:
    cont = False
    i = -1
    for a in actions:
        i = i+1
        if a.name() == action_name:
            cont = True
            break

    if cont:
        selFeatures = layer.selectedFeatures()
        if len(selFeatures) > 0:
            for feature in selFeatures:
                actionManager.doActionFeature(0,feature)
        else:
            qgis.utils.iface.messageBar().pushMessage("Processing Error"," No features selected.",level= QgsMessageBar.WARNING,duration=5)
    else:
        qgis.utils.iface.messageBar().pushMessage("Processing Error"," Wrong layer action defined.",level= QgsMessageBar.WARNING,duration=5)
else:
    qgis.utils.iface.messageBar().pushMessage("Processing Error"," No layer action found.",level= QgsMessageBar.WARNING,duration=5)
1

You can create and run various actions from the Attribute Table for each feature. If you go to Layer Properties > Actions and add a new action, there is an option which allows you to run the action from the attribute table:

Create action

Once you have created an action, you can choose to show it as a combo box:

Show in attribute table

Now when you open your attribute table, you can see a new field containing the actions:

Attribute table

If you want your action to be run on multiple selected features, you will need to include this into your action (if possible). In my example, I am using Python to print the values from the first field for all selected features. So if I select multiple features and then click on any action combo box, it will print the values of the selected features:

Selected features action


However, there seems to be a Run layer action when you right-click an attribute but unfortunately, I can't seem to find much information concerning this. I have posted this as a new question as I am also curious:

What is this 'Run layer action' and how to use it?

  • Thank's for your reply. IMHO, a clear table context Action would be better, telling the user it's running on all selected features. Looks like "Run layer Action" is only a text line/separator in the context menu. – christoph Apr 25 '17 at 10:53
  • @christoph - Most welcome, perhaps there is a more direct approach but not one I'm familiar with. And you could be right about the Run layer Action just being a separator, you could consider posting that as an answer to the question :) – Joseph Apr 25 '17 at 10:59

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.