3

Is there a way to change display name of layer without changing layer name ? The field 'displayed as' is disabled and based on the field 'layer name'.

layer settings

4
  • What is the purpose of having two layer names? Aug 27, 2020 at 7:18
  • The goal is to allow the user to change the display name of the layer dynamically without damaging the code be executed in the background
    – Omri
    Aug 27, 2020 at 7:37
  • If you check out the 'Information' tab, you should see that the layername will not have changed. But it still begs the question of why there is a 'Displayed as' box. Im wondering if it has something to do with any language settings for your environment?
    – nr_aus
    Aug 27, 2020 at 7:43
  • What is even weirder is that if you write in the 'Layer name' field you can see that the 'displayed as' field has light formatting (just capitalized first letter of words as far as i can tell) but it is overriden by the 'Layer name' field once you save the changes Aug 27, 2020 at 8:14

1 Answer 1

1

You need to use the layer id to call a specific layer instead of the layer name. Changing the name does not change the id:

project = qgis.core.QgsProject.instance()
tree = project.layerTreeRoot().children()
layer = tree[0].layer()

print(f'type={type(layer)}')  # type=<class 'qgis._core.QgsVectorLayer'>
print(f'id={layer.id()}')     # id=New_scratch_layer_83348484_7916_4fe4_a137_2b98949ba488
print(f'name={layer.name()}') # name=New scratch layer

layer.setName('new name')     

print(f'id={layer.id()}')     # id=New_scratch_layer_83348484_7916_4fe4_a137_2b98949ba488
print(f'name={layer.name()}') # name=new name
1
  • Great solution! thanks
    – Omri
    Aug 27, 2020 at 10:04

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.