I'm developing a QGIS plugin and I need some help with styling layers in different canvas objects.

In my dialog window I've got two QgsMapCanvas objects canvas1 and canvas2. Layers from registry are loaded to both objects (the same list of layers).

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
    # appending layer to list

Now in both canvas objects I've got the same map composition. Next I want to change layer style only in canvas1. I've tried:

layers = canvas1.layers()
# get polygon layer
layer = layers[0]
# set properties
properties = {"color": "255,0,0", "outline_color": "255,0,255"}
s = QgsFillSymbolV2.createSimple(properties)
layer.setRendererV2( QgsSingleSymbolRendererV2(s))

But this layer style is changed in all of the canvas objects (canvas1, canvas2 and main canvas in Qgis).

How could I change styles for only one canvas in the most efficient way?

  • 1
    Should layer = layer[0] be layer = layers[0]? Commented Apr 23, 2015 at 2:45

1 Answer 1


Your code is uncomplete and not well formated. However, this adaptation seems to work as it is espected:

canvas1 = iface.mapCanvas()

layers = []

n = canvas1.layerCount()

for i in range(n):

canvas2 = canvas1

properties = {"color": "255,0,0", "outline_color": "255,0,255"}

symbol = QgsFillSymbolV2.createSimple(properties)

renderer = QgsSingleSymbolRendererV2(symbol)




After loading three times the world_borders shapefile at the Map View of QGIS, the code was run in the Python Console. Only the layer[1] had the renderer (layer[0] was unmarked to facilitate visualization).

enter image description here

  • 1
    I know that layer can be load three times, but I don't want to do this. I want to set multiple renderers to single layer, each renderer for different canvas.
    – dmh126
    Commented Apr 23, 2015 at 15:33
  • It was only an example. Now, it's more clear your requirement. However, edit your post to complete and put the correct format in your code. Thanks.
    – xunilk
    Commented Apr 23, 2015 at 16:04

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.