Where is the QGIS action for the “deselect” toolbutton in the 'Attributes" toolbar? I do not see this action in the QgisInterface class or do not recognize it by name. Is it missing from iface?

I'd like to use this action in a plugin directly instead of the button on the toolbar.

  • I believe the Deselect function is from the QgsVectorLayer class. – Joseph Jul 24 '15 at 13:48
  • Yes, That is probably what I mean. The toolbar turns on/off with 'Attributes' in the View/Toolbars menu item and the tooltip is "Deselect Features from all Layers'. In any case, I do not see it in iface. – Worth Lutz Jul 24 '15 at 13:55
  • Where did the comment I responded to go? Yes, 'UnSelect' is probably what I mean. – Worth Lutz Jul 24 '15 at 14:01
  • Yes sorry, I realised that you meant another button on the main interface (there is an Unselect all button from the Attribute Table interface) so I removed it from the comment. – Joseph Jul 24 '15 at 14:09
  • Yes, the one on the Attribute Table interface has the same icon. I am looking for the on the main interface in the 'Attributes' toolbar. – Worth Lutz Jul 24 '15 at 14:23

You can get the attributes toolbar and iterate through its actions (buttons) until you get the "deselect" one. Then you can trigger it.

Try this in your QGIS Python console:

for a in iface.attributesToolBar().actions(): 
  if a.objectName() == 'mActionDeselectAll':
| improve this answer | |
  • 1
    Thanks, This will work. I'll just have to search for the action in my setup instead of just getting it from iface. – Worth Lutz Jul 24 '15 at 16:26

I was stuck on the same issue. I found in http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/vector.html#selecting-features


it will just select an empty list and so clear selection.

| improve this answer | |

In QGIS 3.4 using Python Console, also is

| improve this answer | |
  • Really Easy! This could/(should?) be something like: vlayer = iface.activeLayer() vlayer.removeSelection() – Erich Purpur Jul 3 at 12:01

Brute force ;-) A new selection overrides the old one. Then simply deselect by selecting nothing.

for layer_id in QgsMapLayerRegistry.instance().mapLayers():
layer = QgsMapLayerRegistry.instance().mapLayer(layer_id)
    layer.setSelectedFeatures([feat.id() \
        for feat in layer.selectedFeaturesIterator() if feat.id() < 0])

Maybe there is a more distinguished method, but it works.

| improve this answer | |

I guess, the removeSelection thing also works!

for layer in layers():
| improve this answer | |

For QGIS 3 it will be



| improve this answer | |

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.