3

I am trying to add a feature then trying to change shape and size of feature like below:-

layer = QgsVectorLayer('Point?crs=EPSG:4326', 'point' , "memory")
pr = layer.dataProvider()
pr.addAttributes([QgsField("ID", QVariant.Int)])
pt = QgsFeature()
layer.startEditing()
point1 = QgsPoint(float(lon),float(lat))
pt.setGeometry(QgsGeometry.fromPoint(point1))
pt.setAttributes([inputid])
pr.addFeatures([pt])
layer.updateExtents()
layer.commitChanges()
QgsMapLayerRegistry.instance().addMapLayers([layer])
layer = qgis.utils.iface.activeLayer()
symbol_layer = QgsSimpleMarkerSymbolLayerV2()
symbol_layer.setSize(5.0)
symbol_layer.setColor(QColor(255,0,0,255))
layer.rendererV2().symbols()[0].changeSymbolLayer(0, symbol_layer)

but this give me error :-

layer.rendererV2().symbols()[0].changeSymbolLayer(0, symbol_layer)
TypeError: expected string or buffer
4

Using only the relevant parts of your code and adding only one more line:

from PyQt4.QtGui import QColor
layer = qgis.utils.iface.activeLayer()
symbol_layer = QgsSimpleMarkerSymbolLayerV2()
symbol_layer.setSize(5.0)
symbol_layer.setColor(QColor(255,0,0,255))
layer.rendererV2().symbols()[0].changeSymbolLayer(0, symbol_layer)
properties = symbol_layer.properties()
layer.triggerRepaint()

I get the expected result without any problem. It seems that you set the symbol layer in the right way (in fact, the last line in my code simply repaints the layer).

  • the above code works fine in python console but when i add this to my plugin it still gives this error :- layer.rendererV2().symbols()[0].changeSymbolLayer(0, symbol_layer) TypeError: expected string or buffer – vaibhav Dhiman May 8 '17 at 8:37
  • @vaibhavDhiman you didn't specify it, but I'm not sure that the error it is related to it. – mgri May 8 '17 at 8:39

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.