3

I'm looking for a way of preventing/warning users to select another layer without saving the current one they were using.

The only potential solution I found is an autosave plugin but it won't exactly fit to my question.

I'm using QGIS las palmas.

0

1 Answer 1

3

You could connect a function to a signal which is fired whenever the current layer changes. You could add some logic to this function which would always be selected and if you tried to select another layer, it would trigger the Toggle Editing button which if any changes were made would prompt the user to save or discard the changes.

def check_edit():
    layers = iface.editableLayers()
    if layers:
        iface.legendInterface().setCurrentLayer(layers[0])
        iface.actionToggleEditing().trigger()

iface.legendInterface().currentLayerChanged.connect(check_edit)

Edit:

You could create a startup.py script so that the above code is executed when GOMap is initiated. You will need to modify the code slightly to import the relevant module:

from qgis.utils import iface

def check_edit():
    layers = iface.editableLayers()
    if layers:
        iface.legendInterface().setCurrentLayer(layers[0])
        iface.actionToggleEditing().trigger()

iface.legendInterface().currentLayerChanged.connect(check_edit)

And save this as a python script in your C:/Users/you/.qgis2/python/ directory.

8
  • Thanks Joseph, nonetheless my knowledge in coding this is far from advanced and from what I've done I got an error 'iface not defined'. what I did: open layer properties of one layer > fields > python init function = 'provide code in this dialog" > paste your code > function name = check_edit. Maybe what I asked go beyond my knowledge but I'm always keen to learn and for what I understood : do I need to add changes to your code?
    – FranckT
    Commented Oct 1, 2019 at 16:52
  • 1
    add "from qgis.utils import iface" at the top of the file
    – etrimaille
    Commented Oct 1, 2019 at 18:15
  • 1
    But I don't think you should add this in layer properties. This code will work for all layers anyway, so if you setup this code for each layer properties, then this code might be executed many times! Use it from project macro, or python startup.py script or python console. It depends what you want to do exactly.
    – etrimaille
    Commented Oct 1, 2019 at 18:18
  • 1
    I'd like it to execute for each layers indeed. when I copy the code inside one layer, it runs an error before I open the form but indeed the code works when I want to change layer without saving. Using it a startup.py or project macro would solve this issue ? I,ve never done it, I'll try to find it out
    – FranckT
    Commented Oct 1, 2019 at 22:20
  • 1
    Perfect ! Thank you
    – FranckT
    Commented Oct 8, 2019 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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