Skip to main content
3 of 3
improve code
J. Monticolo
  • 15.9k
  • 1
  • 29
  • 65

The values are in the QGIS3.ini and under QGIS, in the properties > Advanced tab > core > Layout > searchPathsForTemplates.

I modify the values with the following code :

new_template_dir = "/home/me/my_custom_template_dir"
app = QgsApplication.instance()
params = QgsSettings(
    app.qgisSettingsDirPath() + "QGIS/QGIS3.ini", QSettings.IniFormat
)
params.beginGroup("/core/Layout")
# retrieve the current directories
existing_dir = params.value("searchPathsForTemplates")
# append the new one in the list
if existing_dir is None:
    existing_dir = [new_template_dir]
elif isinstance(existing_dir, str):
    if new_template_dir != existing_dir:
        existing_dir = [existing_dir, new_template_dir]
    else:
        existing_dir = [existing_dir]
elif isintance(existing_dir, list):
    if new_template_dir not in existing_dir:
        existing_dir.append(new_template_dir)

# setting the new list
params.setValue("searchPathsForTemplates", existing_dir)

The code can be improved by verifying the new_template_dir exists for example but for me, it works.

J. Monticolo
  • 15.9k
  • 1
  • 29
  • 65