<div>In a local python plugin I'm trying to allow Undo\Redo edits. For that I set a <b>beginEditCommand()</b>, <b>endEditCommand() and destroyEditCommand().</b></div><div><b><br></b></div><div>But I'm not being able to make it work. If you press undo, all features added by the plugin are still there. And worst, If I stop editing and discard changes, features disappear from attribute table, but as I start edit again they reappear.</div>
<div><br></div><div>Please note that "l_acao" layer is a non-spatial table, and l_uni_ges is a polygon vector layer.</div><div><br></div><div>I you will, please see my code below.</div><div><br></div><div>Thanks,</div>
<div><br></div><div>Alexandre Neto</div><div><br></div><div>----</div><div><br></div><div>def run(self):</div><div>    # Define "acções" and "unidades de gestão" Layers from project</div><div>    l_acao = QgsMapLayerRegistry.instance().mapLayer(u'acoes20130312143950563')</div>
<div>    l_uni_ges = QgsMapLayerRegistry.instance().mapLayer(u'unidadesdegestao20130312143304288')</div><div>    </div><div>    # Get layer default values from provider</div><div>    # (this avoids problems with unique keys)</div>
<div>    provider = l_acao.dataProvider()</div><div>    temp_feature = QgsFeature()</div><div>    attributes = {}</div><div><br></div><div>    for j in l_acao.pendingAllAttributesList():</div><div>        if not provider.defaultValue(j).isNull():</div>
<div>            attributes[j] = provider.defaultValue(j)</div><div>        else:</div><div>            attributes[j] = None</div><div><br></div><div>    temp_feature.setAttributeMap(attributes)</div><div><br></div><div>   <b> # start edit command to allow undo\redo</b></div>
<div><b>    l_acao.beginEditCommand("Add new actions")</b></div><div>    </div><div>    # open feature form and waits for edits</div><div>    if self.iface.openFeatureForm(l_acao, temp_feature):</div><div>        new_attributes = temp_feature.attributeMap()</div>
<div>        </div><div>        # replicate "acção" record for each</div><div>        # of the selected "Unidades de gestão"</div><div>        uniges_ids = [feature.attributeMap()[1] for feature in l_uni_ges.selectedFeatures()]</div>
<div><br></div><div>        for uniges_id in uniges_ids:</div><div>            new_attributes[1] = uniges_id</div><div>            temp_feature.setAttributeMap(new_attributes)</div><div>            new_feature = QgsFeature(temp_feature)</div>
<div>            l_acao.addFeature( new_feature )</div><div><br></div><div>        <b>l_acao.endEditCommand()</b></div><div>    else:</div><div>        # print "Cancelled"</div><div>        <b>l_acao.destroyEditCommand()</b></div>