5

I'm writing a plugin that interacts with external software (hydrological models).

One of the dialogs has a lot of entries (parameters to pass to the hydrological software).

Once the user has set up all the parameters and the dialog is closed, when the same dialog is re-opened all the previous parameters are gone and the user has to type them again.

What I'd like to achieve is to save all the entries in the dialog so the user knows the old parameters added.

Plugin QGIS2threejs has this functionality but I'm not able to add the code.

And, is it also possible to store the UI parameters (a kind of screenshot) in an external file to that another user can load it and automatically all the fields will be filled?

I saw this answer but I'm not sure I can solve my problem in the same way.

6

Right, you need to use QSettings to store values across QGIS sessions. QSettings is multi-platform, so you don't need to worry about where it stores the data on Linux, on Windows, or on Mac, it handles it for you.

You need to call QSettings in this way:

from PyQt4.QtCore import QSettings 
settings = QSettings() 

You can use QSettings() every time you need it, from any Python (file) module of your plugin.

To store values:

settings.setValue( "/myPlugin/currentTab", self.tabWidget.currentIndex() )

To retrieve values:

settings.value( "/myPlugin/raster/path", "", type=str )
settings.value( "/myPlugin/config/layersOff", type=bool )
settings.value( "/myPlugin/currentTab", type=int )

TIP: If the path you are using is too long, you can open a block like this:

settings.beginGroup( "/myPlugin/config" )

Then assign/read several values in this way:

settings.setValue( "numLayers", n ) # Saved in /myPlugin/config/numLayers
settings.setValue(...)
settings.setValue(...)

And finally, close the block:

settings.endGroup()

As always, the best way to get to know how to best use QSettings in a plugin is to read other people's plugins.

Additionally, you can have a look at the API docs: http://doc.qt.io/qt-4.8/qsettings.html

  • Hi @Germàn.. Thanks for the super quick answer.. Maybe I did not understand.. I can make an instance of QSettings() and pass it as parameters also the ui file or should I call the instance each time in the UI python related file for the values I want to store? Because I have a lots of different objects (Lines, ComboBox, etc..).. Anyway, thanks for the hint! – matteo May 19 '16 at 12:52
  • 1
    Have a look at how I store and retrieve QSettings in my LoadThemAll plugin: github.com/gacarrillor/loadthemall/blob/… It might give you and idea. In each file you need to store/read settings, you can call QSettings(), which will give you access to the settings store. However, nothing prevents you from calling QSettings() from each function you need, as it will give you the same object always. – Germán Carrillo May 19 '16 at 13:05
  • 1
    You don't have to call the QSettings() constructor with a parameter, because the application and organisation parameters mentioned in the API documentation linked to by Germán are provided by QGIS (at github.com/qgis/QGIS/blob/…, for example) – til_b May 19 '16 at 13:10
  • 1
    Thanks @til_b, that's right. If, on the contrary, you were working on a Python Standalone Application, you would need to assign such values by yourself. – Germán Carrillo May 19 '16 at 13:13
  • 1
    Right, functions are optional, of course, you could save everything in one function and read it in another function. I use restoreControls and restoreBaseSettings to read stored settings and update my plugin UIs according to what was saved. If there is nothing saved (i.e., first time the user runs my plugin) I set some default values. In summary, when your plugin opens, you check for stored values, if they exist you apply those values in your UI, but if they don't you apply some default values. When the user makes changes, you store those values and the process starts again. – Germán Carrillo May 19 '16 at 15:17

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.