[Qgis-developer] Update map after adding new feature

Martin Dobias wonder.sk at gmail.com
Thu Jul 10 14:19:29 EDT 2008


On Wed, Jul 9, 2008 at 4:53 PM, Alex Bruy <voltron at ua.fm> wrote:
>
> Hi, all!
> I'm newbie in Qgis and Python plugin development. I'm attempt to write
> Python plugin that read some exchange format and create new polygon features
> in PostGIS vector layer. For inserting features used code:
>
> vLayer = QgsVectorLayer("D:/test",  "test",  "ogr")
> ft = QgsFeature()
> ft.setGeometry(QgsGeometry.fromPolygon([lstCoords]))
> ft.addAttribute(0, QVariant(attr0))
> ft.addAttribute(1, QVariant(attr1))
> ft.addAttribute(2, QVariant(attr2))
> ft.addAttribute(3, QVariant(attr3))
> vLayer.addFeature(ft)
>
> lstCoords - is list of QgsPoints; attr0...attr3 - some strings with
> attributes.
> My first problem is: after inserting no feature appear on the map window and
> no new records in the layer attribute table. But when this layer reloading,
> new features are visible and accessible. I'm try to use startEditing(),
> commitChanges(), setModified() and updateExtents() methods of QgsVectorLayer
> but this don't help. I'm think that I must rerender map after adding new
> feature...  but I don't understand how to do this. Can anyone give little
> example of updating map after adding new features to layer?

Alex,

you should really update map canvas to see the change. This piece of
code should do that:
iface.getMapCanvas().refresh()

(where "iface" should be reference to QgisInterface object which is
passed to plugin on initialization)

Regards
Martin


More information about the Qgis-developer mailing list