Friday, 2 December 2016

qgis - Is there any way to freeze the layer names in the TOC?


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

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...