2

I've stuck on a issue about getting a list of loaded layers. I've two layers loaded, one spatial (point, line or area) and another one non spatial (postgresql table). I've tried this code but it lists only the spatial layers:

layername = []
for i in iface.mapCanvas().layers():
    layername.append(i.name())
print(layername)

I've tried QgsProject.instance().layerTreeRoot()

root = QgsProject.instance().layerTreeRoot()
ids = root.findLayerIds()
print(ids)

but it gives me a layer name plus an id. How can I get a list of layer names from all loaded layers (spatial and non-spatial) in QGIS 3?

3

You can try

layerList = QgsProject.instance().layerTreeRoot().findLayers()
for layer in layerList:
    print(layer.name())

That will give you a list of layer names

Check out the QgsLayerTree class for more info

  • Thanks @Tim its solve my problem. I've also tried with layerTreeRoot().findLayersIds() but it gives the name plus id – Francisco Camello Feb 13 at 14:46

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.