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.
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.
Sign up to join this communityI 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.
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.
You can reference/embed the layers from a second file. (by using the "Layer - Embed Layers and groups" menu entry)
Layers that are embedded like that are read-only (so you can't rename them). You can move them around temporarily but those changes don't get saved.
Sassá, i'm not an expert but here's what i would do :
At startup maintain a lookup table containing the layer id and the corresponding name u want to be left unchanged.
Then i would use the layerNameChanged signal with a line of this kind :
self.curLayer.layerNameChanged.connect(self.watchName)
u do that for every layer of your project.
then in the slot "watchName" u try a :
def watchName(self):
self.curLayer.setLayerName('the_right_name_coming_from_your_lookup_table')
i didn't write all the necesseray code to create the lookup table.
Hope this can help ...