3

I tried to use this code from the QGIS 2 version in QGIS 3:

li = iface.legendInterface()
li.setLayerVisible(myLayer, False)

I found QgsAnnotation but can not use it.

Taras
  • 32,823
  • 4
  • 66
  • 137
Mustafa Uçar
  • 1,581
  • 18
  • 29

1 Answers1

6

In QGIS 3 If you run:

iface.legendInterface()

you get an AttributeError

Traceback (most recent call last):
  File "C:\OSGEO4~1\apps\Python36\lib\code.py", line 91, in runcode
  exec(code, self.locals)
File "<input>", line 1, in <module>
AttributeError: 'QgisInterface' object has no attribute 'legendInterface'

Look at: https://qgis.org/api/api_break.html

QgsLegendInterface was removed. It was replaced by layer tree API (QgsLayerTreeNode class and others). Methods that deal with custom actions in main window's layer tree context menu were moved to QgisInterface: • addLegendLayerAction() moved to QgisInterface::addCustomActionForLayerType() • addLegendLayerActionForLayer() moved to QgisInterface::addCustomActionForLayer() • removeLegendLayerAction() moved to QgisInterface::removeCustomActionForLayerType()

If you want to hide the layer:

 QgsProject.instance().layerTreeRoot().findLayer(lyr.id()).setItemVisibilityChecked(False)
Taras
  • 32,823
  • 4
  • 66
  • 137
Alfredo Garcia
  • 418
  • 2
  • 9