<div dir="ltr">Alex,<div><br></div><div style>You can also set the field using this syntax:</div><div style><br></div><div style>f = QgsFeature()</div><div style>f['id'] = 0</div><div style>f[0] = 0</div><div style>

<br></div><div style>That has worked for me in the past unless something has changed since I tested it last.</div><div style><br></div><div style>- Nathan</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">

On Wed, Feb 27, 2013 at 9:06 PM, Alexander Bruy <span dir="ltr"><<a href="mailto:alexander.bruy@gmail.com" target="_blank">alexander.bruy@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Hi all,<br>
<br>
during plugin update to new vector API I found some strange<br>
behaviour with creating new features in vector layer/file writer.<br>
There is nice description about how to retrieve features and<br>
attributes from existing vector layer, but I can't find any<br>
information about creating new ones.<br>
<br>
Here is my test code for Python console:<br>
<br>
>>>from PyQt4.QtCore import *<br>
>>>fields = QgsFields()<br>
>>>fields.append(QgsField("id", QVariant.Int, "", 20))<br>
>>>fields.append(QgsField("value", QVariant.String, "", 255))<br>
>>>fields.append(QgsField("area", QVariant.Double, "", 20, 6))<br>
>>>fields.append(QgsField("perim", QVariant.Double, "", 20, 6))<br>
>>>w = QgsVectorFileWriter("/home/alex/test.shp", "utf-8", fields, >>>QGis.WKBPolygon, None)<br>
>>>f = QgsFeature()<br>
>>>f.setAttribute("id", QVariant(0))<br>
False<br>
>>>f.setAttribute(0, QVariant(0))<br>
False<br>
<br>
It is impossible to add attribute by its name or index. In API docs<br>
I found method setFields() that used to "assign a field map with<br>
the feature to allow attribute access by attribute name". But when<br>
I assign field map to feature<br>
<br>
>>>f.setFields(fields)<br>
<br>
and try to add attribute again by name or id I get crash<br>
(stacktrace attached).<br>
<br>
It is possible to set all attributes at once using<br>
<br>
>>>f.setAttributes([QVariant(0), QVariant("text"), QVariant(2.3), QVariant(100.456)])<br>
<br>
Is this correct or maybe I miss something?<br>
<br>
Thanks<br>
<span class="HOEnZb"><font color="#888888">--<br>
Alexander Bruy<br>
</font></span><br>_______________________________________________<br>
Qgis-developer mailing list<br>
<a href="mailto:Qgis-developer@lists.osgeo.org">Qgis-developer@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/mailman/listinfo/qgis-developer" target="_blank">http://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
<br></blockquote></div><br></div>