[Qgis-user] Adding folder location to Browser - Favorites with Python

Seggie, Graeme Graeme.Seggie at sepa.org.uk
Sat May 23 10:28:25 PDT 2020


I actually got a response on Stack Exchange regarding this query, so in the interests of sharing the response...

Code to run in QGIS Python console is:

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

browserModel.reload()

Ref:
https://gis.stackexchange.com/questions/362638/qgis-python-to-add-folder-location-to-browser-favorites



Hello,

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 "<input>", line 1, in <module>
  File "<string>", line 3, in <module>
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 "<input>", line 1, in <module>
  File "<string>", line 3, in <module>
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.

Regards,
Graeme

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20200523/5859f0b7/attachment.html>


More information about the Qgis-user mailing list