I have seen this type of question posed a few times on this site. The solutions that I have gleamed from these questions has helped me. But I have not been able to get my script to produce a result that I would like.

I have a layer in my QGIS 3.10 map called "warn", the actual file path and name is "C:\temp\warn1.shp". I am writing a Toolbox to perform a series of processes on this "warn"-file, with the intention that this file will be called something different in the future.

Some Examples that I have used:

I set up the parameters for the Toolbox as described in the QGIS Template:


I call this input in the main script:

Polygon = parameters['INPUT']

When I try and confirm the name of the input using a print statement, a value is returned, but the name contains what appears to be an identifier:

print (Polygon)



When I use the Polygon variable in the definition of the layer:

layer = QgsProject.instance().mapLayersByName(Polygon)[0]

I get an "index out of range" error. Which is expected, as the name does not exist in the map.

How can I return the name of the layer, as it appears in Layer View (Table of Contents)?

In this case I would like "warn" returned.

  • 1
    layer_name = Polygon.split("_")[0] ? Commented Sep 28, 2020 at 8:02
  • @J.Monticolo thanks for the suggestion. Unfortunately in some of the tests I have done, the name might be two words or one. I know I could force it one way or the other, but that would be my last option. I would still prefer the ability to get the name as it appears in the ToC. Commented Sep 28, 2020 at 8:59

2 Answers 2


I've achieved this by a little workaround :

from qgis.utils import iface  # add this in the list of imports
class My_model(QgsProcessingAlgorithm):

    def initAlgorithm(self, config=None):
        # ...

    def processAlgorithm(self, parameters, context, model_feedback):
        # ...
        lyr_list = iface.layerTreeView().layerTreeModel().rootGroup().layerOrder()
        lyr_dict = {lyr.id(): (lyr.name(), lyr) for lyr in lyr_list}
        if parameters['INPUT'] in lyr_dict:
                polygon = lyr_dict[parameters['INPUT']][0]
                layer = lyr_dict[parameters['INPUT']][1]
  • 1
    I used your code and included: if parameters['INPUT'] in lyr_dict: print(lyr_dict[parameters['INPUT']]) layer = QgsProject.instance().mapLayersByName(lyr_dict[parameters['INPUT']])[0] Commented Sep 29, 2020 at 0:33
  • @KeaganAllan: I improved the answer, from the lyr_list, you can access directly to the layers, so I construct a tuple with it. Commented Sep 29, 2020 at 7:05

@Keagan Allan answered to his own question

Using the accepted answer I added the following to my code:

lyr_list = iface.layerTreeView().layerTreeModel().rootGroup().layerOrder()
lyr_dict = {lyr.id(): lyr.name() for lyr in lyr_list}
if parameters['INPUT'] in lyr_dict:
      polygon = lyr_dict[parameters['INPUT']]
      layer = QgsProject.instance().mapLayersByName(polygon)[0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.