1

I am struggling to get something to work, which would seem to be an easy thing to do, but cannot quite make my understanding of the python docs work in practice.

What I would like to do, is run some python to add some folders to the favorites area in the browser within QGIS. This is to get round an environemental constraint of not being able to save a profile i.e. quickly add a python script to setup the favorites each time log on.

This is what I have tried thus far:

url = "U:\Tyres"
qgis.gui.QgsBrowserDockWidget(addFavoriteDirectory, uri) 

...

Traceback (most recent call last): File "C:\OSGEO4~1\apps\Python37\lib\code.py", line 90, in runcode exec(code, self.locals) File "", line 1, in File "", line 3, in NameError: name 'addFavoriteDirectory' is not defined

AND

url = "U:\Tyres"
qgis.gui.QgsBrowserDockWidget(addFavorite, uri) 

...

Traceback (most recent call last): File "C:\OSGEO4~1\apps\Python37\lib\code.py", line 90, in runcode exec(code, self.locals) File "", line 1, in File "", line 3, in NameError: name 'addFavorite' is not defined

Should I be calling the addFavorite code differently? This is within the Python console and script area of same.

2
  • A bit unclear from the documentation, there's a way to do this using a File Explorer: iface.mainWindow().findChild(QDockWidget, 'Browser').addFavoriteDirectory() But passing in a URI as an argument I believe must be done through the QgsBrowserModel, as they mention here: qgis.org/pyqgis/master/gui/… – 15Step May 22 '20 at 14:24
  • Thanks - that does work, but doesn't get me to the quick way of applying a set of folders, as each would have to be manually added. – Graeme May 22 '20 at 14:35
2

To know about startup.py, look at https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/intro.html#run-python-code-when-qgis-starts

Now, you know more, to load automatically at each startup, in startup.py, do

from qgis.utils import iface

browserModel = iface.browserModel()
dirs = ['U:\dir1', 'U:\dir2', 'U:\dir3', 'U:\dir4']
for dir in dirs:
    browserModel.addFavoriteDirectory(dir)

browserModel.reload()

If you are not happy with default startup.py path due to restrictions on each computer, you may configure in QGIS.bat file the environment variable PYQGIS_STARTUP.

You could set it to PYQGIS_STARTUP=%UserProfile%\startup.py and you will just need to copy your startup.py file in C:\Users\<username> (see https://www.tenforums.com/tutorials/3234-environment-variables-windows-10-a.html to understand where %UserProfile% is coming from)

3
  • Brilliant, thank you! That works a treat as the code itself within QGIS and I will explore the startup.py in the environment too. – Graeme May 23 '20 at 17:23
  • 1
    Can you accept the answer? – ThomasG77 May 23 '20 at 17:57
  • Yes, done now - apologies, had missed that. – Graeme May 24 '20 at 9:40

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.