I'm trying to implement a function that would warn the user when he changes active layer and has just made changes on it that his changes have not been saved.
I found this forum: Prevent QGIS user to change layers without saving? Which inspired me. I tried to translate it to QGIS 3 (see the code below).
def check_edit(): layers = iface.editableLayers() if layers: layers = [layer for layer in QgsProject.instance().mapLayers().values()] iface.setActiveLayer(layers) iface.activeLayer() iface.actionToggleEditing().trigger() iface.currentLayerChanged.connect(check_edit)
But the result of it is not as expected because the changes are not saved. The changes appear, but the layer is not saved.
So I kept a little different code to which I added a
pushMessage which warns the user which layer is concerned.
def check_edit(): layers = iface.editableLayers() if layers: iface.messageBar().pushMessage("Attention : ", "Couche "+ iface.activeLayer().name() + " en édition et non sauvegardée !", level=Qgis.Warning, duration=7) iface.activeLayer() iface.actionToggleEditing().trigger() iface.currentLayerChanged.connect(check_edit)
I therefore do not find my code very efficient in the sense that the user is warned but these modifications are not saved.
So I would like to know if it is possible to display this pop-up (screenshot) which would leave the choice to the user to save or not his modifications?