Writing and Reading Python Plugin Settings to/from QGIS Project File
For some plugins it’s necessary to save the settings inside the QGIS project file. Saving is done with this simple one-liner:
QgsProject.instance().writeEntry(pluginName, setting, value)
Then you just need to save the project.
Reading is performed with one of the following functions:
QgsProject.instance().readEntry (pluginName, setting) # for strings QgsProject.instance().readNumEntry (pluginName, setting) QgsProject.instance().readDoubleEntry (pluginName, setting) QgsProject.instance().readBoolEntry (pluginName, setting) QgsProject.instance().readListEntry (pluginName, setting)
You’ll find the corresponding API documentation at: http://doc.qgis.org/stable/classQgsProject.html. As you can see, you can only read/write simple data types. To allow the plugin developer to save more complex plugin settings, I filed an enhancement request.
To handle all those different read functions in a convenient way, I created the following functions:
def readSetting(self,func,setting): """read a plugin setting from QgsProject instance""" value,ok = func('pluginName',setting) if ok: return value else: return None def readSettings(self,setting,value): """read plugin settings from QgsProject instance""" # map data types to function names prj = QgsProject.instance() functions = { 'str' : prj.readEntry, 'int' : prj.readNumEntry, 'float' : prj.readDoubleEntry, 'bool' : prj.readBoolEntry, 'pyqtWrapperType' : prj.readListEntry # QStringList } dataType = type(value).__name__ return = self.readSetting(functions[dataType],setting)
readSettings() has to be supplied with the name of the setting and an example or default value for the setting (from which we can determine the data type of the setting). Of course this can be done in many different ways. In Time Manager plugin, readSettings() receives a dictionary of all settings that have to be read. The function then loops through the dictionary and reads the available settings.
is it possible to help me create a ui attribute lister that shows me the attributes in a form style instead of a tabular style? it is so I can view the necessary data do fill in the fields, without having to scroll sideways looking for the fields.
thanks
carlos
In Layer Properties – General, you can specify an ‘Edit UI’. To create the UI, use Qt 4 Designer. The input field names should be same as the attribute names.