In Where does QGIS Plugin "Customize Toolbar" save its changes? it was pointed out, that "Customize Toolbars" saves the changes in a file called ".CustomToolBars". This file is stored in the home directory of the user, so the path is something like "c:\users\xyz"

I am running a mobile version of QGIS and I would like to change the location of this file to the current directory where the plugin is installed (or any other directory on the USB drive). I use this mobile version for teaching and need to have the path set dynamically so the file will be found no matter of the path of the mobile QGIS.

I have only very little knowledge of programming (and especially Python), but I had a look at the python code and I think that the code lines for defining the location are to be found in line 50 and 51 of "CustomToolbar.py":

[50] self.userhome = os.path.expanduser('~')
[51] self.filepath = self.userhome + '//.CustomToolBars'

In order to set the location dynamically to the current directory of the plugin I changed it as follows:

[50] self.userhome = os.path.dirname(__file__)

But when starting QGIS it fails to load the plugin and gives the following errors:

Traceback (most recent call last):
  File "H:/GIS/usbgis/apps/qgis2/apps/qgis-ltr/./python\qgis\utils.py", line 219, in startPlugin
    plugins[packageName] = package.classFactory(iface)

File "H:\GIS\usbgis\config_lite/python/plugins\CustomToolBar\__init__.py", line 10, in classFactory
    from CustomToolbar import CustomToolbar

  File "H:/GIS/usbgis/apps/qgis2/apps/qgis-ltr/./python\qgis\utils.py", line 478, in _import
    mod = _builtin_import(name, globals, locals, fromlist, level)

  File "H:\GIS\usbgis\config_lite/python/plugins\CustomToolBar\CustomToolbar.py", line 52
    self.userhome = os.path.dirname(__file__)
IndentationError: unexpected indent

Actually, I am a bit lost and don't quite know how proceed further.


I found the solution for the problem by myself - it was just caused by my lacking knowledge of Python and the importance of the indents. The error message "Indention Error: unexpected indent" gave me the hint to search deeper for the problem...

The error occured because I did a copy & paste in Notepad++ of line [50] to change it exactly the way I suggested in my post. I did make a copy of the code line because a wanted to keep the original code commented out by # as a fallback.

What I did not know was, that copy & paste of code lines apparently does not keep the original indents and makes blanks...

So, when I assured that the indents were correct the script worked exactly as I wanted it: it set the path to the ".CustomToolbar"-file to the current directory of the plugin.

By the way, this changing of the variable "self.userhome" also has to be made in line [54] the script "CustomToolbarDialog.py". By this the saving path is also set dynamically to the current directory of the plugin.

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.