There are two things I have coded, re-coded and re-re-coded through all my plugins: the management of the settings and the management of combo boxes associated to layers and their fields.

I have decided to write two generic python modules to solve these tasks to avoid reinventing the wheel every time.

The first one is called QGIS setting manager.
This module allows you to:

  • manage different types of settings (bool, string, color, integer, double, stringlist)
  • read and write settings in QGIS application or in the QGIS project
  • automatically set widgets from corresponding setting
  • automatically write settings from widgets of a dialog

This means that the class of a dialog dedicated to editing the plugins settings can be reduced to just a few lines.
You just have to name widgets according to settings and the module automatically detect the widgets, sets/reads the value from the widget and read/write the settings accordingly.

A setting class would look like this

from qgissettingmanager import *

class MySettings(SettingManager):
    def __init__(self):
        SettingManager.__init__(self, myPluginName)
        self.addSetting("myVariable", "bool", "global", True)

reading and write settings are performed by doing

self.settings = MySettings()
self.settings.setValue("myVariable", False)
myVariable = self.settings.value("myVariable")

and a dialog looks like this

class MyDialog(QDialog, Ui_myDialog, SettingDialog):
    def __init__(self):
        QDialog.__init__(self)
        self.setupUi(self)
        self.settings = MySettings()
        SettingDialog.__init__(self, self.settings)

You can find a complete howto here and look at the code on github.

The second module is called QGIS combo manager. This module autmatically manages combo box widgets for layers, fields of vector layers and bands of raster layers.
You can associate a field combo to a layer combo: as soon as the layer has been modified, the fields are updated to the current layer.

Associating a combo box to layers and another one to its fields would look like this:

from qgiscombomanager import *

self.layerComboManager = VectorLayerCombo(self.layerComboWidget)
self.myFieldComboManager = FieldCombo(self.myFieldComboManager, self.layerComboManager)

You can find a complete howto here and look at the code on github.