To accomplish this, you have to take a two-step approach. The first step is to create the field, the second one is to loop over all your features, evaluate the expression and update the attribute for each feature.
vl
is your QgsVectorLayer
QGIS 2.0
from PyQt4.QtCore import QVariant
from qgis.core import QgsField, QgsExpression, QgsFeature
vl.startEditing()
#step 1
myField = QgsField( 'myattr', QVariant.Int )
vl.addAttribute( myField )
idx = vl.fieldNameIndex( 'myattr' )
#step 2
e = QgsExpression( 'Column * 3' )
e.prepare( vl.pendingFields() )
for f in vl.getFeatures():
f[idx] = e.evaluate( f )
vl.updateFeature( f )
vl.commitChanges()
QGIS 1.8
from PyQt4.QtCore import QVariant
from qgis.core import QgsField, QgsExpression, QgsFeature
vl.startEditing()
#step 1
myField = QgsField( 'myattr', QVariant.Int )
vl.addAttribute( myField )
idx = vl.fieldNameIndex( 'myattr' )
#step 2
e = QgsExpression( 'Column * 3' )
e.prepare( vl.pendingFields() )
f = QgsFeature()
vl.select( vl.pendingAllAttributesList() )
while vl.nextFeature( f ):
vl.changeAttributeValue( f.id(), idx, e.evaluate( f ) )
vl.commitChanges()