5

I am writing on an update of the qgis2leaf plugin. It would be great to store the settings of the plugin UI (like which checkboxes are checked, which strings are used) in a separate file (like xml or sth. else). secondly it would be great to load such a file and set the UI with the parameters in this file.

Does anyone know a plugin for qgis that might already have such a functionality so I can use it as a template?

2

4 Answers 4

5

Yes, you can store settings in separate files/ways with QSettings, and within the same Q(Core)Application. This makes it ideal for what you are looking to do.

When initializing your PyQGIS plugin's QSettings, you will want to do something like this for a standard settings setup:

my_settings = QtCore.QSettings(QtCore.QSettings.IniFormat,
                               QtCore.QSettings.UserScope,
                               'MyOrganization', 'MyApp')

Or for a standalone file setup:

my_settings = QtCore.QSettings("/path/to/mysettings.ini",
                               QtCore.QSettings.IniFormat)

Edit: if you are going to use a standalone file, consider storing it in the QgsApplication.qgisSettingsDirPath() directory, e.g. usually resolves to ~/.qgis2/.

What you DO NOT want to do, like many PyQGIS plugin developers have often done, is the following:

my_settings = QtCore.QSettings()

Since your plugin is running inside of an existing QApplication, this will (probably) return a reference to QGIS's QSettings object, which is not necessarily a good place to store plugin settings. Users are often apt to remove those settings in an attempt to fix an issue with the application.

1
  • sounds good, I'll give it a try as it is prob. better than parsing the whole UI in a csv file like described below.
    – Riccardo
    Commented Sep 24, 2014 at 6:50
5

You can use QSettings to read and write settings. It takes care of storing the settings on all platforms without you having to find a place to write a file.

I used this functionality in the Google Maps Engine Connector plugin to store UI settings https://github.com/NathanW2/mapsengine-qgis-connector/blob/master/plugin/settings.py

1
  • Hi Ujaval. Thanks for the hint. I thought about this as well but sometimes you need different settinmgs for different projects. So having different "settings files" can be an advantage.
    – Riccardo
    Commented Sep 21, 2014 at 12:09
1

My plugin uses this also: http://plugins.qgis.org/plugins/geopunt4Qgis/

The dialog that saves the settings: https://github.com/warrieka/geopunt4Qgis/blob/master/geopunt4QgisSettingsdialog.py

2
  • there is no possibility to store different settings in different files, is there?!
    – Riccardo
    Commented Sep 21, 2014 at 12:07
  • Not with QSettings
    – warrieka
    Commented Sep 21, 2014 at 17:15
1

I haven't found a plugin that store it's settings in a customizable way. ATM I am extracting all the current values and store them in a csv file:

my_settings = {"Foldername": self.outFolderName, "Basemap": self.basemapname}
with open('filename.csv', 'wb') as f: 
    w = csv.DictWriter(f, my_settings.keys())
    w.writeheader()
    w.writerow(my_settings)

The file can be loaded as well:

with open('filename.csv'), mode='r') as infile:
    reader = csv.DictReader(infile)
    for rows in reader:
        #now restore the settings by mapping the file:
        self.ui.foldername.setText(rows['Foldername'])
        index_basemap = self.ui.basemapbox.findText(rows['Basemap'])
        self.ui.basemapbox.setCurrentIndex(index_basemap)

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.