How to add Feature to vlayer in qgis 1.3.0 in Python?

Discussion of plugin development

How to add Feature to vlayer in qgis 1.3.0 in Python?

Postby KSJ on Mon Nov 02, 2009 12:00 pm

Hello, everyone.
I try these codes to add feature to a "momery" vectorlayer under Qgis 1.3.0 :
(Reference to http://wiki.qgis.org/qgiswiki/PythonBindings and change some command with different Qgis version)
Code: Select all
from PyQt4.QtCore import *
from PyQt4.QtGui import *
v01=QgsVectorLayer("Point","KSJ","memory")
pr=v01.dataProvider()
pr.addAttributes({"name":"string"})
pr.addAttributes({"ageee":"int"})
pr.addAttributes({"size":"double"})
feature=QgsFeature()
feature.setGeometry(QgsGeometry.fromPoint(QgsPoint(10,10)))
feature.setAttributeMap( { 0 : QVariant("Johny"), 1 : QVariant(20), 2:QVariant(0.3) } )
pr.addFeatures( [ feature ] )
v01.updateExtents()
QgsMapLayerRegistry.instance().addMapLayer(v01)


It can be done but - dataprovider has no field showing(included field "id")

But I can use codes like:
Code: Select all
iface.activeLayer().dataProvider().nextFeature(QGSFEATURE)
QGSFEATURE.attributeMap()[0].toString()

to get its value. That means it really has,but doesn't show, right?

I think there must be some problems with my conception.
could anyone help me? Thanks a lot.

KSJ
KSJ
Newbie
Newbie
 
Posts: 2
Joined: Mon Nov 02, 2009 11:05 am

Return to Plugins

Who is online

Users browsing this forum: No registered users and 1 guest