[Qgis-developer] featureAdded(QgsFeatureId) signal

Paolo Corti pcorti at gmail.com
Thu Nov 28 14:24:44 PST 2013


Hi devs

Within a plugin, I need to make some kind of geometric topological
manipulation on every feature as soon as it is added to the layer by
the user.

My best guess was to use the featureAdded(QgsFeatureId) signal and to
modify the geometry within it.
Unluckily it looks like the geometry is effectively updated only when
the edits are saved by the user. In that case the featureAdded is
emitted again and it works correctly.

Workflow:
* the user add a feature within an edit session
* the featureAdded signal is emitted a first time, the qgsfId is
negative, the changeGeometryValues results to True but nothing happens
* the user save the edits
* the featureAdded signal is emitted a second time, the qgsfId is now
positive, the feature having been stored, changeGeometryValues results
to True and the geometry is effectively updated

Is there a way to implement what I need (having the feature's geometry
modified just the first time the signal is emitted)?

Here is the stub code I am using:

QObject.connect(self.mylayer, SIGNAL("featureAdded(QgsFeatureId)"),
self.featureAdded)

def featureAdded(self, qgsfId):
        request=QgsFeatureRequest()
        request.setFilterFid(qgsfId)
        feat = self.mylayer.getFeatures(request).next()
        .... (some more code here)
        self.mylayer.beginEditCommand("Feature manipulation")
        self.mylayer.dataProvider().changeGeometryValues({ feat.id() :
newgeom })
        self.mylayer.endEditCommand()
        self.canvas.refresh()

thanks in advance
p

-- 
Paolo Corti
Geospatial software developer
web: http://www.paolocorti.net
twitter: @capooti
skype: capooti


More information about the Qgis-developer mailing list