Thank you very much for your help!
I'm going to make a research about "queued signal/slot connection instead of 
direct connection" since that is completely new for me :-)
Meanwhile, now that I know the reason of the error, my workaround consists in 
delaying the handling of the feature addition using the editCommandEnded slot. 
This is the relevant code:
      
def onFeatureAdded(self, fid):
    if fid < 0:
        self._addedFeatures.append(fid)

def onEditCommandEnded(self):
    while self._addedFeatures:
        fid = self._addedFeatures.pop()
        self._handleAdded(fid)

def _handleAdded(self, fid):
    guid_pol = str(uuid4()) # RFC 4122 UUID v4
    try:
        self.layer.beginEditCommand(u"Assign UUID")
        self.layer.changeAttributeValue(fid, 
self.layer.fieldNameIndex('guid_pol'), guid_pol)
        self.layer.endEditCommand()
    except:
        self.layer.destroyEditCommand()
        raise

So far seems to work.

-----Mensaje original-----
De: Martin Dobias [mailto:wonder...@gmail.com] 
Enviado el: domingo, 06 de marzo de 2016 9:53
Para: Manuel Hernández Martínez
CC: Qgis-developer@lists.osgeo.org
Asunto: Re: [Qgis-developer] QGIS crashes when doing a rollback after modifying 
values of an user-added feature with pyQGIS

Hi Manuel

On Fri, Mar 4, 2016 at 10:05 PM, Manuel Hernández Martínez 
<mhernand...@agenciamedioambienteyagua.es> wrote:
>
> self.iface.mapCanvas().currentLayer().featureAdded.connect(self._debug
> _onFeatureAdded)
>
>     def _debug_onFeatureAdded(self, fid):
>         layer = self.iface.mapCanvas().currentLayer()
>         layer.beginEditCommand("Set UUID")
>         print layer.changeAttributeValue(fid, 
> layer.fieldNameIndex('guid_pol'), 'some_random_uuid') # prints True
>         layer.endEditCommand()
>
>  [...]
>
> When I edit the layer, if I add a feature the field 'guid_pol' is set 
> to 'some_random_uuid' as expected. I can commit the changes without a 
> problem, however if instead of committing the changes I try cancelling 
> them QGIS crashes with error 0xC0000005 (Access Violation).

Unfortunately this is currently a limitation of our current editing system... 
When an editing signal (like "featureAdded") is emitted, doing some extra 
editing actions will corrupt the undo stack because the undo command of the 
previous action is not yet finished. A possible workaround could be to use 
queued signal/slot connection instead of direct connection (which is the 
default), so the slot execution will be delayed, and the undo stack will get to 
a consistent state.

Regards
Martin
_______________________________________________
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer

Reply via email to