4

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?

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

1 Answer 1

5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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