1

I'm trying to get color values from layers inserted into canvas. I used this code but it seems that I'm doing something wrong:

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
    sym = layer.rendererV2().symbols()[0]
    fill = QgsSimpleFillSymbolLayerV2().fillColor()
    p=fill.getRgb()
    print(p)

I always get (0,0,255,255) as a result.

enter image description here

Also this is how it looks Layer properties from one of the polygones in canvas.

enter image description here Is there any way or code to do this right?

I would also like to add that second fill color is added using this code:

sym = layer.rendererV2().symbols()[0]
fill = QgsSimpleFillSymbolLayerV2()
fill.setFillColor(QColor.fromRgb(result[0], result[1], result[2]))
sym.appendSymbolLayer(fill)
1
  • The new edited part is a bit different question, there is problem with symbol index [0] which returns QgsSimpleFillSymbolLayer object. And you need append fill to QgsFillSymbol object. So use .symbols() without index. Hope this helps. Also next time please ask different question in separate posts, and not editing the existing one, thanks ;)
    – Oto Kaláb
    Apr 11, 2020 at 8:51

1 Answer 1

1

You assign layer.rendererV2().symbols()[0] to sym variable

sym = layer.rendererV2().symbols()[0]

but then call .fillColor() on QgsSimpleFillSymbolLayerV2() not on your symbol sym. So try:

fill = sym.fillColor()

In QGIS 3 it should be something like this (example for active layer):

lay = iface.activeLayer()
sym = lay.renderer().symbol()[0]
fill = sym.color()
print(fill.getRgb())
7
  • As you can see in 'Layer properties' I I have 2 fill colors added but when I execute your code I get values for only one of them and that is one that is not active on my canvas. Blues is on my canvas but python console shows purple. How can I get to other one?
    – Ivan
    Apr 11, 2020 at 7:23
  • @Ivan You get the first one because of index [0] in lay.renderer().symbol()[0]. For second fill color use [1]. Also note that both fill parts of symbols are "activated", you actually can't deactivate part of symbol, you only don't see the second because the first completely cover it.
    – Oto Kaláb
    Apr 11, 2020 at 8:35
  • I think problem is that fillcolor() and color() and different methods for different properties of layer. That is why I get wrong values. When I try to execute sym.fillColor() i get error 'QgsFillSymbolV2' object has no attribute 'fillColor' and when I use fill = sym.color() like you wrote I get values but not the fill values that I'm looking for. Also if I change to lay.renderer().symbol()[1], I get error
    – Ivan
    Apr 11, 2020 at 9:57
  • The problem is if I set values like this:
    – Ivan
    Apr 11, 2020 at 9:58
  • sym = layer.rendererV2().symbols()[0] fill = QgsSimpleFillSymbolLayerV2() fill.setFillColor(QColor.fromRgb(result[0], result[1], result[2])) sym.appendSymbolLayer(fill)
    – Ivan
    Apr 11, 2020 at 9:58

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.