1

I have several *.qgs projects and I want to open each project, add layer, export composer and close this project withot saving. Goto next project and repeat this operations. But I did not find any close() method for QgsProject. How I can close *.qgs without saving? Something like this:

project = QgsProject.instance()
for prjFile in prjFiles:
   project.read(QFileInfo(prjFile))
   #do something with project
   #close project instance without saving and goto next project
3
  • Why do you need to close it? Surely it's overwritten by the new project file by the next iteration. Commented Oct 17, 2018 at 6:59
  • When I open second project (project.read(QFileInfo(prjFile2)) I see the composes of prjFile1 and did not see any layers of prjFile2 in Layer Panels. So It seems to me, prjFile1 not close completely. May be it is need to delete object QgsProject.instance() befor open new project?
    – oreshnik
    Commented Oct 17, 2018 at 7:37
  • Try moving project = QgsProject.instance() into your loop and then call del project on the last line of the loop to flush the object properly (call its destructor). You might also consider using a with block like with QgsProject.instance() as project: which will automatically destroy the object correctly. Commented Oct 17, 2018 at 7:39

1 Answer 1

2

Solution my problem for others:

from qgis.core import QgsProject
from PyQt4.QtGui import qApp

#Open project1
project = QgsProject.instance()
project.read(QFileInfo('C:/project1.qgs'))

project.clear() #Close project1
qApp.processEvents() #Wait untill  GUI Update

#Open project2
project = QgsProject.instance()
project.read(QFileInfo('C:/project2.qgs'))

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.