5

I create and set some global variables in my QGIS plugin as below:

QgsExpressionContextUtils.setGlobalVariable('variable_name', 'some_value')

The name is unique and specific, so different from any QGIS global variable name and not a regular word. There are 5 variables set in the plugin, some on the init and some later. Values passed are always strings. The problem is that on some computers values are assigned to wrong variable names. Eg.:

it should be:
'name_one': 'variable_one'
'name_two': 'variable_two'
'name_three': 'variable_three'
'name_four': 'variable_four'

it is:
'name_one': 'variable_three'
'name_two': empty
'name_three': 'variable_one'
'name_four': 'variable_three'
# totally mixed up

Has anyone experienced it? I'm using different subversions of QGIS 2.18 on Windows 7 and Ubuntu 16.04.3. Specifically:

Does not work:

  1. Ubuntu 16.04 x64, QGIS 2.18.5
  2. Ubuntu 16.04 x64, QGIS 2.18.13
  3. Windows 7 x86 (Virtualbox on [4]), QGIS 2.18.18

Works

  1. Ubuntu 16.04 x64, QGIS 2.18.17
  2. Windows 7 x86 (Virtualbox guest on [4]), QGIS 2.18.16
  3. Windows 7 x64, QGIS 2.18.9 32-bit

EDIT: So far I got it working (on OSs that it hadn't before) by declaring and setting to an empty string all of the global variables, that are going to be used later, and it was done at the very beginning of the plugin code. Without purging QGIS config it kind of 'repaired itself' this way. Drawback 1) - it requires knowing in advance what globals exactly will you have. Drawback 2) - I still have no clue what happened here and while it works now, I wouldn't be surprised if it magically stops one day.

11
  • In which computers/os does the problem occur? I tried in Win10 x64 with QGIS 2.18.17 / 3.0.0-Girona / 3.1.0-Master and Ubuntu 16.04 with QGIS 2.18. And no problem. Apr 8, 2018 at 2:42
  • Hi Kadir, I updated post with versions tested, I'll be updating it when I test more.
    – adamczi
    Apr 9, 2018 at 11:24
  • seems to be done differently in 3.0, stored as "/variables/variable_name=value" . In 2.18 these are being packed/unpacked into lists. What do you see in Settings > Options > Advanced under /variables/names and /variable/values ?
    – Steven Kay
    Apr 9, 2018 at 12:54
  • Hi, in "names" I get variable names, each repeated hundreds(?) of times, and values are empty. Which is odd, because I can see values in "Variables" panel in Project Properties and in Settings (although mixed up) - i.imgur.com/FStzNSk.png
    – adamczi
    Apr 9, 2018 at 18:21
  • 1
    I store a big dictionary, and it's fine. I am on MacOS, but users are in Ubuntu, windows. Maybe you can use dict to store it? Apr 10, 2018 at 2:26

1 Answer 1

1

I have similar experience storing dictionary in Project variable. My trick is make a several variable from the dict with some certain format. For example:

a = {
  'b': 'c',
  'd': {
     'e': 'f'
  },
  'g': ['x', 'y', 'z']
}

will be stored as:

  1. 'a__b' = 'c' (common type)
  2. 'a__d__e' = 'f' (dict)
  3. 'a__g__1' = 'x' (list)
  4. 'a__g__2' = 'y' (list)
  5. 'a__g__3' = 'z' (list)

From this, you can get your dictionary back (with some trick, I never implement the method to get the dictionary back since we decided to ommited the list and the dicionary). See my code here: https://github.com/inasafe/inasafe/blob/4871612b8cb0e2611721d96c6d9662b1f9b5db78/safe/gui/widgets/dock.py#L1408

I hope this help or give you some idea.

2
  • How many project variables do you store this way? I expect to have thousands of them, fortunately it's a plain dict with nothing nested.
    – adamczi
    Apr 11, 2018 at 10:20
  • I think it's less than 20, and less than 4 level dictionary. Apr 11, 2018 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.