I would like to freeze the names of layers in TOC. Is there any way to do this via QGIS config or PyQGIS?
I´m using QGIS Wien.
Answer
You can (sort of) achieve it by altering the QGIS layer tree model. Write the following code snippet in a QGIS Python console:
root = QgsProject.instance().layerTreeRoot()
model = QgsLayerTreeModel(root)
model.setFlag(QgsLayerTreeModel.ShowLegend)
model.setFlag(QgsLayerTreeModel.ShowLegendAsTree)
model.setFlag(QgsLayerTreeModel.AllowNodeReorder)
model.setFlag(QgsLayerTreeModel.AllowNodeChangeVisibility)
model.setFlag(QgsLayerTreeModel.AllowLegendChangeState)
model.setFlag(QgsLayerTreeModel.AllowNodeRename,False)
tv=iface.layerTreeView()
tv.setModel(model)
After running the code, renaming layers (and groups) from the layer tree is no longer possible, although it can be done via the Properties dialog or via PyQGIS.
I guess that would be the most you can get.
No comments:
Post a Comment