4

I want to fill vector layer as transparent fill. I am using following code but it fills it with white colour and inner boundaries for vector layer get hid

Code i am using :

   from PyQt4 import QtGui      
   layer = iface.addVectorLayer("path_to_vector_layer", "layer_name", "ogr") 
   myVectorLayer = iface.activeLayer()
   myRenderer  = myVectorLayer.rendererV2()

   if myVectorLayer.geometryType() == QGis.Polygon:

     mySymbol1 = QgsFillSymbolV2.createSimple({'color':'#ffffff', 
                                      'color_border':'#000000',
                                      'width_border':'0.6'})
   myRenderer.setSymbol(mySymbol1)
   myVectorLayer.triggerRepaint()

1 Answer 1

8

Try replacing

'color':'#ffffff'

with

'color':'0,0,0,0'

The last zero is the alpha chanel which controls the level of transparency. So setting it to 0 will set the fill colour with an opacity of 0%.


Tested with the following code:

from PyQt4 import QtGui      
layer = iface.addVectorLayer("path_to_vector_layer", "layer_name", "ogr") 
myVectorLayer = iface.activeLayer()
myRenderer  = myVectorLayer.rendererV2()
if myVectorLayer.geometryType() == QGis.Polygon:
    mySymbol1 = QgsFillSymbolV2.createSimple({'color':'255,0,0,0',
        'color_border':'#000000',
        'width_border':'0.6'})

myRenderer.setSymbol(mySymbol1)
myVectorLayer.triggerRepaint()
iface.legendInterface().refreshLayerSymbology(myVectorLayer)
6
  • 1
    It works! Is there a documentation for the Parameter of createSimple? I mean, how do i know the possible members of that dict? Jan 12, 2017 at 10:14
  • @AndreasMüller - Glad it worked for you :). Hmm I'm struggling to find any documents regarding the parameters for createSimple(). But this post descirbes a number of the parameters involved.
    – Joseph
    Jan 12, 2017 at 10:40
  • That's exactly my experience. As a none C++-Programmer it is hard to find answers studing the source code, like here: qgis.org/api/qgssymbollayerv2utils_8cpp_source.html, line 48, where i could find the answer... So, thank you! Jan 12, 2017 at 10:48
  • @AndreasMüller - If you use the following myVectorLayer.rendererV2().symbols()[0].symbolLayer(0).properties(), it prints out the names and values of the parameters which could be used =)
    – Joseph
    Jan 12, 2017 at 10:56
  • 1
    Very nice! Thank you, again! This could be helpfull with other objects, too! Jan 12, 2017 at 11:00

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.