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).

asked Jun 11, 2018 at 17:49

2 Answers 2

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])
answered Jun 12, 2018 at 20:23
1
  • 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. Commented Jun 13, 2018 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

answered Oct 15, 2019 at 12:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.