[Qgis-developer] displaying memory provider layers

Carson Farmer cfarmer at uvic.ca
Wed Jun 11 15:34:59 EDT 2008


Does anyone know if it's possible to display QgsVectorLayers that use 
the new 'memory' data provider?

I followed Martins example here...
> # create layer
> vl = QgsVectorLayer("Point", "", "memory")
> pr = vl.getDataProvider()
>
> # add fields
> pr.addAttributes( { "name" : "string", "age" : "int", "size" : "double" } )
>
> # add a feature
> fet = QgsFeature()
> fet.setGeometry(QgsGeometry.fromPoint(QgsPoint(10,10)))
> fet.setAttributeMap( { 0 : QVariant("Johny"), 1 : QVariant(20), 2 :
> QVariant(0.3) } )
> pr.addFeatures( [ fet ] )
>
> # show some stats
> print "fields:", pr.fieldCount()
> print "features:", pr.featureCount()
> e = pr.extent()
> print "extent:", e.xMin(),e.yMin(),e.xMax(),e.yMax()
>
> # iterate over features
> f = QgsFeature()
> pr.select()
> while pr.getNextFeature(f):
>        print "F:",f.featureId(), f.attributeMap(), f.geometry().asPoint()
... which all works fine, but when I tried: 
QgsMapLayerRegistry.instance().addMapLayer(vl) QGIS crashes (i.e. 
disappears and leaves me with the 'busy' cursor)?

Any thoughts? Perhaps there is something I have to call before trying to 
add the layer to the map registry?

Thanks, Carson


More information about the Qgis-developer mailing list