Tuesday 29 March 2016

pyqgis - Remove saving notification when use python QGIS at startup


I try to run python at QGIS startup. The script :


from qgis.core import QgsProject

from PyQt4.QtCore import QFileInfo

project = QgsProject.instance()


project.read(QFileInfo('D:/test/qgis/1.qgs'))

I save as startup.py in C:\Users\USER.qgis2\python. The project is successfully opened. There are two layer. A vector and a raster. But, there is a notification : "Do you want to save the current project?". If I click 'Save' or 'Discard', the 1.qgs is saved , and then there is a blank project. If I click 'Cancel', the 1.qgs is still opened. It is disturb a lay user. While I don't want to close the project. I just want to load the project.


How do I remove this notification?



Answer



I believe @Spacedman is correct in that the startup.py script is read before QGIS loads a blank project. The QgisInterface class does contain an initializationCompleted() signal which is emitted once QGIS has finished its initialisation process. We can then connect this to a function which can load your project:


from qgis.core import QgsProject
from PyQt4.QtCore import QFileInfo
from qgis.utils import iface


project = QgsProject.instance()

def load_project():
project.read(QFileInfo('D:/test/qgis/1.qgs'))

iface.initializationCompleted.connect(load_project)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...