This code works well. I used it with a point shapefile (Multi Part) for getting its convexHull Polygon.
layer = iface.activeLayer()
feat = layer.selectedFeatures()[0]
#get geometry
geom = feat.geometry()
convexhull = geom.convexHull()
#Extract CRS from layer
CRS = layer.crs().postgisSrid()
URI = "Polygon?crs=epsg:"+str(CRS)+"&field=id:integer""&index=yes"
#Create polygon layer for convexHull
mem_layer = QgsVectorLayer(URI,
"convexhull",
"memory")
#add Map Layer to Registry
QgsMapLayerRegistry.instance().addMapLayer(mem_layer)
#Prepare mem_layer for editing
mem_layer.startEditing()
#Set feature for convexHull
feat2 = QgsFeature()
#Set geometry for convexHull
feat2.setGeometry(convexhull)
#set attributes values for convexHull
feat2.setAttributes([1])
mem_layer.addFeature(feat2, True)
#stop editing and save changes
mem_layer.commitChanges()
iface.mapCanvas().refresh()
Before running the code in the Python Console (selected features in default yellow color):
After running the code:
Convexhull is a memory layer and it can be saved as shapefile in the directory that you prefer or be used to do a buffer in the same script. Automatically, convexhull becomes the active layer.