5

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
6

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':
    a.trigger()
    break
  • 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
6

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

layer.setSelectedFeatures([])

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

1

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.

1

I guess, the removeSelection thing also works!

legend=iface.legendInterface()
layers=legend.layers()
for layer in layers():
    layer.removeSelection()
1

In QGIS 3.4 using Python Console, also is

iface.activeLayer() 
vlayer.removeSelection()

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.