I made a tool that adds some features into a layer which has to be in an edit session. But also noticed that if I want to stop edit session without saving changes my new features still exist in layer. Used this kind of adding features

vlayer = iface.activeLayer()
if vlayer.isEditable():
    vlayer.dataProvider().addFeatures([feature]) # QgsFeature() with geometry

Looks like Qgis thinks that I add them in non-edit session. Any way to make it waiting for comitting results?

  • You could use vlayer.rollBack(True) to remove any added features which have not been saved. – Joseph Apr 24 at 15:15

According to this document you need to call commitChanges or rollBack methods.

For these methods to work, the layer must be in editing mode. To start the editing mode, use the startEditing() method. To stop editing, use the commitChanges() or rollBack() methods. The first one will commit all your changes to the data source, while the second one will discard them and will not modify the data source at all.

So I guess you need to call rollback() when you are done.

  • Turon, forgot to mention that my lines are within a PyQt tool in which user can perform changes in layer that should be only in edit session. After adding features by this tool I want user to decide save layer changes by closing edit session manually or discard them. – Pavel Pereverzev Apr 24 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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