0

I am working on a QGIS plugin that allows redrawing a selected feature without losing its original attributes.

The plugin works properly and the process ends well but I am receiving a QT error that I can't solve: Qt: QUndoStack::endMacro(): no matching beginMacro()

This is the portion of the code:

  def run(self):
        """Run method that performs all the real work"""
      
               
        if layer is not None:
                                   
            # Check, with messages, that only one feature is selected
            if layer.selectedFeatureCount() == 0:
                QMessageBox.warning(
                None,
                'Replace Geometry Plugin',
                'There is no selection. Select one feature.')
            elif layer.selectedFeatureCount() >= 2: 
                 QMessageBox.warning(None,
               'Replace Geometry Plugin',
                'The selection contains multiple features. Select only one feature.')
                     
            else:
                
                # Turn off the attribute form popup
                form_config = layer.editFormConfig() 
                form_config.setSuppress(QgsEditFormConfig.SuppressOn) 
                layer.setEditFormConfig(form_config)
                
                for feature in layer.selectedFeatures():
                    global old_id
                    old_id = [feature.id()]
                       
                def feature_added(self):
                    
                    # Disconnect from the signal
                    layer.featureAdded.disconnect()
                    
                    listOfIds = [feat.id() for feat in layer.getFeatures()]
                    new_feature_id = [listOfIds[0]]
                    
                    # Calculate the new WKT
                    layer.selectByIds(new_feature_id)
                    for feature in layer.selectedFeatures():
                        new_WKT = feature.geometry().asWkt()
                        layer.removeSelection()
                     
                    layer.startEditing()
                    layer.selectByIds(old_id)
                    for feature in layer.selectedFeatures():
                        final_id = feature.id()
                        new_geometry = QgsGeometry.fromWkt(new_WKT)
                        layer.changeGeometry(final_id, new_geometry)
                    
                    # Remove the new feature
                    layer.removeSelection()
                    layer.selectByIds(new_feature_id)
                    for feature in layer.selectedFeatures():
                        toremove_id = feature.id()
                        layer.deleteFeature(toremove_id)
                        # Save the changes
                        layer.commitChanges()
                    
                    # Reselect the old feature
                    layer.selectByIds(old_id)
                    
                    # Turn on the popup if ever was on
                    form_config = layer.editFormConfig() 
                    form_config.setSuppress(QgsEditFormConfig.SuppressDefault) 
                    layer.setEditFormConfig(form_config)
                    
                    
                # Connect the layer to the signal featureAdded, so when a feature is added to the layer, the feature_added function is called
                layer.featureAdded.connect(feature_added)

                # Set the layer in edit mode
                layer.startEditing()
                    
                #Activate the QGIS add feature tool
                iface.actionAddFeature().trigger()    

Any suggestion to solve it and avoid the error?

Your Answer

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

Browse other questions tagged or ask your own question.