Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible with PyQGIS to catch "add new feature" event before commiting changes on the layer? or at least to access new features before commit.

share|improve this question
up vote 1 down vote accepted

The QgsVectorLayer has quite a number of signals that will may suite your needs. Documentation for 1.8 and master on these links.

Note there are minor changes to the API between 1.8 and master, and it appears that the python bindings have a few issues that are still unresolved.

This sample code defines a couple of listeners, then attaches them to a memory layer. If you start editing the layer you will see each signal triggers a line in the message log.

This code is (mostly) compatible with 1.8 and master and is intended to be pasted into the python console.

from PyQt4.QtGui import *

def logLayerModified( onlyGeometry = None ):
    QgsMessageLog.logMessage( "layer modified" )

def logFeatureAdded(fid):
    QgsMessageLog.logMessage( "feature added, id = " + str(fid) )

def logEditingStarted():
    QgsMessageLog.logMessage( "editing started" )

def logCommittedFeaturesAdded( layerId, addedFeatures ):
    message = layerId + " has features added: "
    for feature in addedFeatures:
        message += str( ) + ", "
    QgsMessageLog.logMessage( message )

layer = QgsVectorLayer( "Point", "Layer Signal Demo", "memory" )    
QgsMapLayerRegistry.instance().addMapLayers( [layer] )

layer.layerModified.connect( logLayerModified )
layer.featureAdded.connect( logFeatureAdded )
layer.editingStarted.connect( logEditingStarted )
layer.committedFeaturesAdded.connect( logCommittedFeaturesAdded )

EDIT: Even with an incomplete set of signals you can probably get the access you require by performing you own analysis.

I have a naive implementation here that will slow down for large feature counts. Depending on your needs you may be able to optimize by reducing the amount of data being handled during the cross referencing. You may get speed gains by omitting the geometry or full attribute table from the query until you have identified the features you are interested in.

Note that this is designed for 1.8, feature iteration has changed significantly for master.

from PyQt4.QtGui import *

def layerModified( onlyGeometry = None ):
    global establishedFeatureIds
    newFeatureIds = []
    feature = QgsFeature()
    while layer.nextFeature( feature ):
        id =
        if not id in  establishedFeatureIds:
            newFeatureIds.append( id )
    message = "New features: "
    for id in newFeatureIds:
        message += str(id) + ", "
    QgsMessageLog.logMessage( message )

def editingStarted():
    global establishedFeatureIds
    establishedFeatureIds = []
    feature = QgsFeature()
    while layer.nextFeature( feature ):
        establishedFeatureIds.append( )

layer = qgis.utils.iface.activeLayer()
if layer is None:
    layer = QgsVectorLayer( "Point", "Cross-reference Demo", "memory" )    
    QgsMapLayerRegistry.instance().addMapLayers( [layer] )

layer.layerModified.connect( layerModified )
layer.editingStarted.connect( editingStarted )
share|improve this answer
Hi Kelly, I forgot to mention that I'm using PostgreSQL layer and I tried to handle featureAdded signal with no luck. QObject.connect(layer, SIGNAL("featureAdded(qint64)"), self.featureAdded). However, editingStarted and editingStopped work ok. So, I'm looking for any solution either to catch featureAdded event or at least to have an access to the new features before commit. – AlexSV Mar 3 '13 at 19:03
Which QGIS version are you targeting? – Kelly Thomas Mar 4 '13 at 1:43
It is 1.8, but think to switch on 1.9 since there are some bugs fixed in the latest build. – AlexSV Mar 4 '13 at 10:02
I did some tests and yes, featureAdded doesn't work for memory and postgres layers in 1.8 and master. However, thank you for the clue, so I can at least find new features now. Unfortunately it is not very effective with big layers, as you noted. It would be nice if it is possible to pick up all changed features from provider or elsewhere, including new, modified and deleted... – AlexSV Mar 6 '13 at 8:39
I'm confused now which version I should proceed with, since if I go with 1.8 it will not work with 2.0 later on. Is there any source with the latest PyQGIS API documentation on the Internet (not for 1.8)? – AlexSV Mar 6 '13 at 8:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.