4

I am trying to stop the edits using the python console of the QGIS and the code QgsTrackedVectorLayerTools.stopEditing(layer,False) where layer is the layer in the edit mode.

I am receiving the following error: NameError: name QgsTrackedVectorLayerTools is not defined

Does anyone know the right syntax for the QgsTrackedVectorLayerTools function?

  • 2
    You will need to import it using from qgis.gui import QgsTrackedVectorLayerTools and then use QgsTrackedVectorLayerTools().stopEditing(layer, True). Unfortunately, this crashes my QGIS and I'm unsure if this is the correct method to stop the editing of a layer. – Joseph Oct 17 '17 at 14:33
  • 1
    Thanks @Joseph.The way you call this method seems correct, but it crashes my Qgis too. – Spyros Oct 18 '17 at 6:36
  • 1
    @Joseph can confirm it crashes my 2.18.12 as well – LaughU Oct 18 '17 at 11:51
2

I would refer to this QGIS documentation page QgsTrackedVectorLayerTools Class Reference

It provides this sample code:

1 /***************************************************************************
2   qgstrackedvectorlayertools.cpp - QgsTrackedVectorLayerTools
3 
4  ---------------------
5  begin                : 16.5.2016
6  copyright            : (C) 2016 by Matthias Kuhn, OPENGIS.ch
7  email                : matthias@opengis.ch
8  ***************************************************************************
9  *                                                                           *  
10  *   This program is free software; you can redistribute it and/or modify    *
11  *   it under the terms of the GNU General Public License as published by  *
12  *   the Free Software Foundation; either version 2 of the License, or     *
13  *   (at your option) any later version.                                   *
14  *                                                                         *
15  ***************************************************************************/
16 #include "qgstrackedvectorlayertools.h"
17 #include "qgsvectorlayer.h"
18 
19 QgsTrackedVectorLayerTools::QgsTrackedVectorLayerTools()
20     : mBackend( nullptr )
21 {
22 }
23 
24 bool QgsTrackedVectorLayerTools::addFeature( QgsVectorLayer* layer, const QgsAttributeMap& defaultValues, const QgsGeometry& defaultGeometry, QgsFeature* feature ) const
25 {
26   QgsFeature* f = feature;
27   if ( !feature )
28     f = new QgsFeature();
29 
30   if ( mBackend->addFeature( layer, defaultValues, defaultGeometry, f ) )
31   {
32     mAddedFeatures[layer].insert( f->id() );
33     if ( !feature )
34       delete f;
35     return true;
36   }
37   else
38   {
39     if ( !feature )
40       delete f;
41     return false;
42   }
43 }
44 
45 bool QgsTrackedVectorLayerTools::startEditing( QgsVectorLayer* layer ) const
46 {
47   return mBackend->startEditing( layer );
48 }
49 
50 bool QgsTrackedVectorLayerTools::stopEditing( QgsVectorLayer* layer, bool allowCancel ) const
51 {
52   return mBackend->stopEditing( layer, allowCancel );
53 }
54 
55 bool QgsTrackedVectorLayerTools::saveEdits( QgsVectorLayer* layer ) const
56 {
57   return mBackend->saveEdits( layer );
58 }
59 
60 void QgsTrackedVectorLayerTools::setVectorLayerTools( const QgsVectorLayerTools* tools )
61 {
62   mBackend = tools;
63 }
64 
65 void QgsTrackedVectorLayerTools::rollback()
66 {
67   QMapIterator<QgsVectorLayer*, QgsFeatureIds> it( mAddedFeatures );
68   while ( it.hasNext() )
69   {
70     it.next();
71     it.key()->deleteFeatures( it.value() );
72   }
73 
74   mAddedFeatures.clear();
1

QgsVectorLayerTools instance is exposed by QgisInterface (iface), you can stop editing with:

iface.vectorLayerTools().stopEditing(layer,False)

iface.vectorLayerTools() by default returns QgsGuiVectorLayerTools. Tested with QGIS 2.18

  • 1
    Good job. It also works with QGIS 3.4. – xunilk Jan 2 at 13:08

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.