11

Is it possible to change the color while adding a vector layer? I have a vector layer with water areas and want to load it with a blue color style. The layer is added with:

QgsMapLayerRegistry.instance().addMapLayer(self.vlayer)

Can I somehow change the color after or while loading?

11

you can use something like this:

symbols = self.vlayer.rendererV2().symbols()
symbol = symbols[0]
symbol.setColor(QColor.fromRgb(50,50,250))

Good job!

| improve this answer | |
7

Finally with this code the change of a layer color is working for me:

QgsMapLayerRegistry.instance().addMapLayer(self.vlayer)       
symbols = self.vrlayer.rendererV2().symbols()
symbol = symbols[0]
symbol.setColor(QtGui.QColor.fromRgb(50,50,250))
qgis.utils.iface.mapCanvas().refresh() 
qgis.utils.iface.legendInterface().refreshLayerSymbology(self.vlayer)

You have to reload the MapCanvas and the layers panel aswell.

| improve this answer | |
  • This code saved me from painfull research => qgis.utils.iface.legendInterface().refreshLayerSymbology(self.vlayer) – Kostadin Nov 17 '17 at 11:51
  • What module do you need to load to get access to rendererV2()? – user2627043 Sep 5 at 2:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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