0

I am using the python console to interact with QGIS 3.0 and have accessed the layout manager using:

lomgr = QgsProject.instance().layoutManager()
lo = lomgr.layouts()[0]
myMapItem = lo.itemById('myMapItem')

I need to lock the layers programmatically as I would if I were using the LayoutManager UI (e.g., select the item, go to Item Properties tab, and check/uncheck the 'Lock Layers' checkbox). I can't find any way of doing this from the documentation (https://python.qgis.org/api/classQgsLayoutItem.html).

1

Call setLayers([layer1,layer2,...]) (https://qgis.org/pyqgis/3.0/core/Layout/QgsLayoutItemMap.html#qgis.core.QgsLayoutItemMap.setLayers) on the map item:

lomgr = QgsProject.instance().layoutManager()
lo = lomgr.layouts()[0]
myMapItem = lo.itemById('myMapItem')
myMapItem.setLayers([layer1, layer2])
  • Thanks for the response. I had tried that approach but setLocked() locks the LayoutItem itself from mouse interactions. I am trying to lock the layoutItem layers which is different. I have found the layer lock setting within the template XML but there doesn't appear to be a python function to perform the layer locks. I'll post more once I have more time to test. Thanks. – okiedev Jun 13 '18 at 21:50
1

sure it is too late but others might find it useful:
The methods you were looking for were probably:

setLayers()   

and

storeCurrentLayerStyles() 

These are methods of class QgsLayoutItemMap.
Consider you have a map object called map1 of a QGIS - Project called project1. To lock all layers of map1 of the current layer tree you could:

map1.setLayers(project1.mapLayers().values())

and then

map1.storeCurrentLayerStyles()

To release the layers you could e.g. simply use an empty dictonary in the setLayers method:

map1.setLayers({})
map1.storeCurrentLayerStyles()

Hope it helped

New contributor
bbsea is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

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.