Hi Marting<br><br><div class="gmail_quote">Dne 19. dubna 2010 15:53 Martin Dobias <span dir="ltr"><<a href="http://wonder.sk">wonder.sk</a>@<a href="http://gmail.com">gmail.com</a>></span> napsal(a):<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Hi Vaclav,<br>
<br>
2010/4/19 Václav Řehák <<a href="mailto:rehakv01@gmail.com">rehakv01@gmail.com</a>>:<br>
<div class="im">> Hi,<br>
><br>
> in my plugin I create a layer using the following code:<br>
><br>
> self.lines = QgsVectorLayer("LineString", "temporary_lines", "memory")<br>
> self.lines.startEditing()<br>
> field = QgsField("label", QVariant.String)<br>
> field.setLength(100)<br>
> ret = self.lines.addAttribute(field)<br>
> QgsMapLayerRegistry.instance().addMapLayer(self.lines)<br>
> self.lines.commitChanges()<br>
><br>
> However, the field is not added to the layer and "ret" is false. I realized<br>
<br>
</div>You can add attributes using this code:<br>
<br>
pr = self.lines.dataProvider()<br>
<br>
# add fields - to preserve correct order they must be added one-by-one (d'oh)<br>
pr.addAttributes( { "name" : "string" } )<br>
pr.addAttributes( { "age" : "int" } )<br>
pr.addAttributes( { "size" : "double" } )<br>
<br>
Your approach should ideally work too, but as I'm looking into the<br>
code, there are some missing bits that prevent your code to work as<br>
well. I'll fix it, but the solution I've posted above will work with<br>
older qgis releases, too.<br></blockquote></div><br>I have tried your suggestion, but it also doesn't work for me:<br><br>self.lines = QgsVectorLayer("LineString", "temporary_lines", "memory")<br>
pr = self.lines.dataProvider()<br> self.lines.startEditing()<br> added = pr.addAttributes( { "label" : "string" } )<br> QgsMapLayerRegistry.instance().addMapLayer(self.lines)<br>self.lines.commitChanges()<br>
<br>I don't see "label" in attributes table of my layer and "added" has value False.<br><br>Thanks,<br><br>Vaclav<br>