7

I want to remove all selection from registered layer in QGIS using Python. I tried to clear the map canvas using:

self.iface.mapCanvas().clear()  
self.iface.mapCanvas().refresh() 

but the selection persists.

15

There is probably a better way to do this, but you can iterate the layers in mapCanvas, and use removeSelection() method.

Something like this:

mc = self.iface.mapCanvas()

For layer in mc.layers():
    if layer.type() == layer.VectorLayer:
        layer.removeSelection()

mc.refresh()
5

This is a another method is given below:

for a in iface.attributesToolBar().actions(): 
  if a.objectName() == 'mActionDeselectAll':
    a.trigger()
    break
1
  • Deselects all the selected features (from different layers) at once. No need for iterating the map layers.
    – Sjs
    Oct 17 '16 at 9:38
1

iface.mainWindow().findChild(QAction, 'mActionDeselectAll').trigger() works also.

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.