3

When a layer is on the edit mode and I click on the exit button of the QGIS project, it asks me if I want to save changes that I made (automatically). So, how can I disable it using PyQGIS? I've tried QgsProject.instance().setDirty(False) but it keeps asking if I want to save my changes. The image below shows the message that pops up.

enter image description here

How to automatically discard changes and never prompt the user to save changes?

I saw simillar questions like this one: Is there is a signal in QGIS when a project is about to be closed?

But it was also an open question.

4
  • 1
    Do you modify the project after using setDirty(False)? Sep 5, 2020 at 8:29
  • Yep, after modifying, I don't want to save my modifications.
    – Samuel
    Sep 5, 2020 at 17:00
  • I use the version 3.10.7
    – Samuel
    Sep 8, 2020 at 14:24
  • Now I updated to version to 3.14.15 and it keeps asking if I want to save changes. :/
    – Samuel
    Sep 8, 2020 at 14:33

1 Answer 1

5

QgsProject.instance().setDirty(False) works just if you don't modify the project after using it.

If you would like it to be effective until closing QGIS, define a method including that and connect the method to isDirtyChanged signal.

def set_dirty_false():
    QgsProject.instance().setDirty(False)

QgsProject.instance().isDirtyChanged.connect(set_dirty_false)
10
  • I did what you said, but it is still asking if I want to save modifications. What I'm doing is: opening the project, modifying it, but when closing I don't want to save the modifications and I don't want the window asking to save my modifications to pop up.
    – Samuel
    Sep 5, 2020 at 17:00
  • @Samuel Did you run the script using QGIS Python Editor? Sep 6, 2020 at 6:39
  • Yep, it keeps asking if I want to save the changes.
    – Samuel
    Sep 6, 2020 at 13:24
  • I added an image of the window that pops up. In case, I want to either disable that window, or autoselect "discard".
    – Samuel
    Sep 6, 2020 at 13:27
  • 1
    @menes I think he didn't miss it, it's just that the question was edited after the answer, changing the base scenario. Sep 10, 2020 at 15:04

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.