2

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
canvas1.setLayerSet(list)
canvas2.setLayerSet(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
  • 1
    Should layer = layer[0] be layer = layers[0]? Apr 23, 2015 at 2:45

1 Answer 1

2

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):
    layers.append(canvas1.layer(i))

canvas2 = canvas1

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

symbol = QgsFillSymbolV2.createSimple(properties)

renderer = QgsSingleSymbolRendererV2(symbol)

layers[1].setRendererV2(renderer)

layers[1].triggerRepaint()

iface.legendInterface().refreshLayerSymbology(layers[1])

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

2
  • 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
    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
    Apr 23, 2015 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.