3

I'm trying to uncheck a layer on the layer panel using PyQGIS such that it is not visible on the project without removing it (like with removeMapLayer).

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
  • Did u search ? I see plenty of question about it on this forum ... [qgis] or [pyqgis] ... – Snaileater Apr 10 '18 at 09:39
  • Yes I did. What I'm looking for is quite simple, just to uncheck the layer from the panel, which is why I say I might have overlooked something. What I find is more advanced stuff like removing buttons, or API action. – wyvernaizor Apr 10 '18 at 09:45
  • 1
    @wyvernaizor - Use something like layer = iface.activeLayer(); iface.legendInterface().setLayerVisible(layer, False) – Joseph Apr 10 '18 at 10:37
  • 1
    @Joseph Aha, thank you so much. That definitely pointed me into the right direction. I failed to mention that I am using QGIS 3 so your solution was a bit outdated, but I have edited my post with the solution. Thanks! – wyvernaizor Apr 10 '18 at 11:14

1 Answers1

4

Solution: See Joseph's reply. For QGIS 3 and up, see below:

There's also a similar question that I didn't find because I looked for different keywords, my bad.

QgsMapCanvasLayer in QGIS 3?

QgsProject.instance().layerTreeRoot().findLayer(lyr.id()).setItemVisibilityChecked(False)