2

For a lay user, when the user opens the dialog to open a project file, it is better if there is no need to go through folders, but access the project folder directly in the folder shown by the dialog.

It can be done by opening the project file and closing QGIS. When QGIS starts and we want to open a project file, QGIS automatically points to the last project folder opened.

I am creating portable QGIS for lay user. I want to create a code in python that will put the code in startup.py. Something like :

Set the project folder : "%~dp0DATA\QGIS2\bin"

So, when the user clicks "open project", QGIS will automatically point to the project folder.

How can I set that project folder in PyQGIS?

3
  • I'm not sure I follow. Do you want the user to load QGIS first, or load from the project file? Or have a project selector thing?
    – Nathan W
    Feb 23, 2017 at 0:03
  • 1
    note: QGIS can take a project on the command line qgis project.qgs
    – Nathan W
    Feb 23, 2017 at 0:04
  • The user will load QGIS first. Then the user will open the project file in the folder. When click open, automatically point the project folder. So, the user doesn't need to find in another folder.
    – mega
    Feb 23, 2017 at 0:11

2 Answers 2

3

If I got you right, you need to set the lastProjectDir QGIS setting:

from PyQt4.QtCore import QSettings    
QSettings().setValue("UI/lastProjectDir","F:/my/project/path/")

Next time you open the "open project file" dialog, it will show you projects directly in the folder you've set.


NOTE: Of course, you need to set a valid folder path. For me, "%~dp0DATA\QGIS2\bin" is not a valid path. You can check if a folder path exists in this way:

import os.path
os.path.isdir( myPath ) 
1
  • German Carillo, the code is work. I use QSettings().setValue("UI/lastProjectDir","%~dp0DATA/QGIS2/bin") for relative path.
    – mega
    Feb 24, 2017 at 0:31
2

iface.addProject(filepath) is the function you need.

https://qgis.org/api/classQgisInterface.html#aac7fba8634c03e0d4c8d4539308db0ca

3
  • iface.addProject(%~dp0DATA\QGIS2\bin), give invalid syntax. I put many qgs files in bin folder.
    – mega
    Feb 23, 2017 at 0:30
  • It takes a path to the the project file. It's not going to magic a UI for you. If you need something to pick a file you need to look at QFileDialog in Qt, use that to select the file and pass it to addProject
    – Nathan W
    Feb 23, 2017 at 0:56
  • iface.addProject("D:/DATA/QGIS2/bin/1.qgs"). It is work. But, when I open any project file, the directory opened is still in C:\Users\USER. I want, QGIS will open in D:/DATA/QGIS2/bin/. Or when in relative path in %~dp0DATA\QGIS2\bin. So?
    – mega
    Feb 23, 2017 at 6:54

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.