8

I have a layer of GPS points (about 60 layers) loaded inside my .qgs file. I want to set all their styles in the same manner using a .qml file without doing it one by one.

Can somebody illustrate initial Python code to do this? I'm relatively new to QGIS (2.16.1 Nodebo) since I came from ArcGIS doing simple batch processing through arcpy. In arcpy, I call the arcpy.mapping module to access my .mxd file and update my layer style through UpdateLayer function.

2 Answers 2

12
for layer in iface.mapCanvas().layers():
    layer.loadNamedStyle('path/to/qml/file')

Or if this won't work, try:

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
    layer.loadNamedStyle('path/to/qml/file')
5
  • 2
    for layer in iface.mapCanvas().layers(): does not work for me, instead I use for layer in QgsMapLayerRegistry.instance().mapLayers().values():. Does it work for you? I'm using QGIS 2.16.
    – Joseph
    Commented Sep 8, 2016 at 10:07
  • @Joseph: Both solutions work for me. (v. QGIS 2.14).
    – dmh126
    Commented Sep 8, 2016 at 10:17
  • Interesting, tested again on 2.14.3 and still nothing. But I believe you as I have used that before ;)
    – Joseph
    Commented Sep 8, 2016 at 10:21
  • 1
    I updated my post and now there are both solutions.
    – dmh126
    Commented Sep 8, 2016 at 10:25
  • 2
    For me on QGIS 2.14.6 also works only second solution
    – Oto Kaláb
    Commented Sep 8, 2016 at 11:44
5

Not directly python solution, but there is a plugin called MultiQml that is exactly doing what you are looking for. After installing you find the plugin in menu Plugins --> MultiQml --> MultiQml. In plugin window select layers, click on Apply style ... button and search for your .qml file.

enter image description here

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.