[Qgis-developer] pyQgis-question

Bernhard Ströbl Bernhard.Stroebl at jena.de
Tue Sep 28 06:05:09 EDT 2010


Hi Luca,

thank you for your reply, which made me trying further, much 
appreciated. Your code works perfect with an existing feature (will use 
it some other place), however my new feature has no id assigned!

For those interested I elaborated the following:

newFeat = QgsFeature()
newGeom = # some magic :-)
newFeat.setGeometry( newGeom )

# important: create attributes at this point with null values...
for att in layer.pendingAllAttributesList():
     newFeat.addAttribute( att, QVariant() )

# ...in order to change attribute's value...
newFeat.changeAttribute( idx, 'string' )

# ...before adding feature to the layer
if layer.addFeatures( [ newFeat ] ):
     layer.setModified()

when a feature is added to the layer a copy of it is added, thus the 
feature is not accessible any more before the layer's changes are 
commited (because the new feature has no id)

Bernhard

luca_manganelli at comune.trento.it schrieb:
> qgis-developer-bounces at lists.osgeo.org scritti il 28/09/2010 08.21.31
> 
>> myIdx = myLayer.fieldNameIndex( 'myfield' )
>> newFeature.changeAttribute( myIdx, newValue )
>> #does not change the field value to new value :-(
> 
>> what is the second argument of changeAttribute supposed to be?
> 
>>From my 'homemade' plugin:
> 
>       self.lay_stradearchi.changeAttributeValue (self.minFeature.id(),
> 
> self.lay_stradearchi.dataProvider().fieldNameIndex("FIELDNAME"),
>                         self.dlgEdit.ui.newCode.text())
> 
> First argument is the FEATURE ID,
> second argument is the index of the field,
> third is the new value.
> 
> HTH.
> 
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
> 
> 
> ________ Information from NOD32 ________
> This message was checked by NOD32 Antivirus System for Linux Mail Server.
> http://www.nod32.com




________ Information from NOD32 ________
This message was checked by NOD32 Antivirus System for Linux Mail Server.
http://www.nod32.com


More information about the Qgis-developer mailing list