I am trying to do field data collection using QGIS. One of the features that I regard as necessary for data management is timestamps. For example, a timestamp can indicate that an inspection record has been updated.

I have figured out how to add triggers using Spatialite to add a timestamp on update, however the timestamp is added when the edited records are saved and not when the identify dialog box is closed. This means that timestamps can be out by many minutes. One solution would be to force the updated record to be saved by using a custom form with an OK button (Can do the form with Qt-Designer but don't know how to add the save command to the button yet.) However my preferred solution would be to take a timestamp for new records, when the point or line is created. The reason for this is if I am surveying a road, I can stand on the road, collect the point and them move to the side to complete data collection.

I have been unable to work out how to get Spatialite to run a trigger that puts in the start of feature editing. Perhaps this is also something that needs to be done by code in a custom form. Is there an On-dirty event or signal in QGIS that indicates when someone starts editing a record and that can be used to capture the time? Could I also ask what the Python code (and what to hook it to) that would put such a timestamp in the timestamp field.


  • Are you happy to stamp the record when the form opens?
    – Nathan W
    Commented Jul 23, 2013 at 0:31
  • yes absolutely.
    – AndrewM
    Commented Jul 23, 2013 at 2:21

1 Answer 1


Note: This will only work in 1.9 (2.0) due to a bug in 1.8 not passing the full feature correctly

Take this code:

from PyQt4.QtCore import QDateTime

def onopen(dialog, layer, feature):
    if feature.id() < 0:
        # This is a new record because it 
        # doesn't have a vaild id yet
        feature['createdon'] = QDateTime.currentDateTime()

Save it somewhere QGIS Python can find it, normally in the same folder as the project is good. Lets call it datestamper.py

In Layer Properties -> Fields set the Python Init Function to datestamper.onopen

enter image description here

Now when ever the form is opened, auto generated or custom, that function will be called and the 'createdon' field will be set to the current date time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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