5

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?

  • 1
    I think what is happening is that your project.read is happening before QGIS has initialised its usual initial project. So when it tries to do that, your project is there, so it asks if you want to save/cancel/etc your project just as it does when you load a new project from the menu. I suspect you'll have to be a bit trickier - maybe your startup code will have to wait for a signal that QGIS is done initialising and then read the project. – Spacedman Jan 30 '17 at 8:24
4

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)
  • @mega - Most welcome, glad it worked :) – Joseph Jan 31 '17 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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