1

I try to use this code from QGIS 2.x version in QGIS 3.0:

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

I found QgsAnnotation but can not use it.

4

In QGIS3 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)

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.