I am working on a script where I want the user to add feature to a layer in edit mode, and then decide whether to keep the newly added feature in the layer or not. I am doing it the following way:
layer.featureAdded.connect(store_feature_id)
def store_feature_id(feature_id):
featureIter =
layer.getFeatures(QgsFeatureRequest().setFilterFid(feature_id))
for feat in featureIter:
feature = feat
reply = QMessageBox.question(iface.mainWindow(),
'Save Changes?',
"Save your changes?",
QMessageBox.Yes, QMessageBox.No)
if reply == QMessageBox.Yes:
layer.updateFeature(feature)
layer.commitChanges()
The problem I have is that the command layer.commitChanges
will trigger the layer.featureAdded
condition again, thus this program will run in an endless loop. I am not sure why, because I expected that the feature add event to only emit once when the feature is initially added to the layer.
This problem can probably be solved by using an extra bool variable as flag, but I am wondering if there is a more elegant solution using the available pyqgis functions?