2

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.

  • "freeze names" ... u mean forbid any name change ? – snaileater May 19 '15 at 16:39
  • Exactly @snaileater. – Sassá May 19 '15 at 16:55
3

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.

|improve this answer|||||
  • Grateful @gcarrillo! I think that this solves the question. I'll try. Thanks again! – Sassá May 20 '15 at 11:49
  • 1
    Wonder gcarrillo. Done! – Sassá May 20 '15 at 12:08
  • Glad to know it worked for you! – Germán Carrillo May 20 '15 at 14:24
2

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.

|improve this answer|||||
  • Thanks @Max but not quite that. I need to keep editable layers. – Sassá May 19 '15 at 18:46
  • Ok, didn't see that in your question. Hope you find out! – Max May 19 '15 at 18:49
2

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 ...

|improve this answer|||||

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.