2

I am writing a qgis plugin to display spectral information stored in a geodatabase (Spectre Viewer). For this, I need calibration factors to get from channel values to energies. I want the end users to be able to set their own calibration factors that will vary depending on the layer and field name selected to plot the spectra.

So I need to store some persistent data. I would prefer to either store it in the directory where the plugin is stored, store it in the active project or store it in the general QGIS settings. I do see pros and cons of all those designs.

The data I want to store will be a tree structure:

Layer1 -- field1_1 -- [set of numeric calibration values ]
       \- field1_2 -- [set of numeric calibration values ]
Layer2 -- field2_1 ...

Are there any way to store custom data in the QGIS project file or in general QGIS settings?

For a while I was thinking about storing the data as a json file in the plugin directory, then I discovered that this file was erased when I update the plugin, which kind of makes it useless. I can of course save the file anywhere the user running qgis can save a file, but I feel that would be a bit too much "messing around where I am not supposed to be messing around".

1
  • 1
    I think you need to show us what kind of data you want to store. What is the data structure? You can save data in Project variable, in global variable, in layer variable, in QgsSettings, in a directory, or in a database. You can do whatever you want.
    – etrimaille
    Oct 30, 2019 at 21:22

1 Answer 1

3

In the end I used the central qgis settings for storage of my data, so now my code has a few constructs like

 s=QgsSettings()
 self.scene.bcalib=s.value(self.pluginname+"/defaultb", 0)

and

 s=QgsSettings()
 s.setValue(self.pluginname+"/"+layername+"_"+fieldname+"_a", self.scene.acalib)      

This makes sense as the same data will be used whenever the same dataset is used. I considered two other ways of saving data:

As a json file in the plugin directory. That worked fine, but it turned out the file was erased when the plugin was upgraded which was not acceptable for my use.

In many ways, the best way of storing the data would be in a table in the same database as the data is stored. The problem is that this would make it more complicated to set up the database.

Your Answer

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

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