如何在pyQGIS中更改矢量层的颜色?


10

添加矢量图层时是否可以更改颜色?我有一个带有水域的矢量层,并希望将其加载为蓝色样式。该层添加有:

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

加载后或加载时,我能以某种方式更改颜色吗?

Answers:


11

您可以使用以下方式:

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

做得好!


7

最后,通过这段代码,更改图层颜色对我来说是可行的:

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)

您还必须重新加载MapCanvas和“图层”面板。


这段代码使我免于痛苦的研究=> qgis.utils.iface.legendInterface()。refreshLayerSymbology(self.vlayer)
Kostadin
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.