When writing python scripts, QGIS does not save them when the user saves his main project with the ctrl+S
command or with Project->Save
. Besides, when leaving QGIS, the program does not warn about unsaved changes in python scripts even if it warns about unsaved changes to the project and offers to save them.
Is there any option to change this behaviour?
I should add that using the dialogue option to save the project when quitting does indeed save the project, but not the python scripts. Those are simply lost.
Best Answer
If you want to save the current python script in the Python Editor each time you save your project, type the following into the console:
This essentially connects the projectSaved() signal to the saveScriptFile() function in the python console.
Credit to Germán Carrillo for his answer in this post in which the above answer was based on.
Edit:
One method of automatically saving all scripts is to slightly modify your processing console.py file which can be found in your QGIS directory. For example:
Find the
def saveScriptFile(self):
function and replace the first line:with the following and indent everything after:
So that it looks like:
Save the file.
Now we need to create a startup.py file in your
/.qgis2/python/
directory, this file is loaded when QGIS starts up so it will run any code saved inside. Include the following code:Save the file.
Now, hopefully, when you load QGIS and save a project, all opened scripts in the console editor should be saved.
(Note: I'm quite certain you can iterate through the opened scripts in the console editor but haven't found it yet. This would be a much better and less intrusive method as it avoids altering the source code.)