7

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.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Worth Lutz
  • 533
  • 4
  • 10
  • 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 Answers6

9

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
Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178
  • 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
8

In QGIS 3.4 using Python Console, also is

iface.activeLayer() 
vlayer.removeSelection()
leotv
  • 439
  • 6
  • 10
7

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.

Clement
  • 765
  • 1
  • 6
  • 23
4

I guess, the removeSelection thing also works!

legend=iface.legendInterface()
layers=legend.layers()
for layer in layers():
    layer.removeSelection()
drama
  • 367
  • 1
  • 12
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.

Detlev
  • 4,608
  • 19
  • 26
0

For QGIS 3 it will be

layer.deselect(feature_id_here)

https://qgis.org/pyqgis/3.10/core/QgsVectorLayer.html?highlight=deselect#qgis.core.QgsVectorLayer.deselect

Abhijit Gujar
  • 2,750
  • 3
  • 29
  • 43