3

So after writing in a projectfile as in the cookbook in QGIS 2.14 with.

finfo = QFileInfo(filename)
project.write(finfo)

I want to open that project in the main window. So I try:

project.read(finfo)

But the project doesn't open. How can I open the .qgs file I've created in the window?

  • 1
    Did you close the project before trying to open it? Did you receive errors when running the code? – Joseph May 30 '17 at 14:31
  • I tried with another project open. And with no project open @joseph – VilyaSeeFour May 30 '17 at 14:44
3

It is strange but if you write a project and read directly after the project does not load correctly. You can solve this strange using the clear() method, before you read the project file:

Simple script

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *
from qgis.utils import iface

project = QgsProject.instance() 
mFile=QFileInfo("C://Users//fran//Desktop//test.qgs")

project.write(mFile)

project.clear()

project.read(mFile)
  • Is this explained anywhere in the qgis cookbook? Which is the closest thing to a pyqgis api I've been able to find. – VilyaSeeFour Jun 1 '17 at 6:51
  • I don't know, I found this solution, observing the method in the qgis core – Fran Raga Jun 1 '17 at 7:17
  • Unfortunatley it also clears the project I noticed later so it isn't optimal. – VilyaSeeFour Jun 2 '17 at 8:51
  • clears the project??of course,but you load again same project or anyone. – Fran Raga Jun 2 '17 at 10:47

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.