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
  • Why do you need to close it? Surely it's overwritten by the new project file by the next iteration. – Michael Stimson Oct 17 '18 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 Oct 17 '18 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. – Michael Stimson Oct 17 '18 at 7:39
1

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'))
|improve this answer|||||

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.