10

I'd like to save the project-dependent configuration of my Python plugin inside the QGIS project file. Is this possible and what are best practices to do so?

2 Answers 2

10

This does not work in QGIS 3.0, see answer below

Original answer

This should do it:

QgsProject.instance().writeEntry(plugin_name, property, value)

Then you just need to save the project.

Reading is performed with one of the following functions:

readEntry (plugin_name, property)
readNumEntry (plugin_name, property)
readDoubleEntry (plugin_name, property)
readBoolEntry (plugin_name, property)

Doc: http://qgis.org/api/classQgsProject.html

0

The original answer does not work in QGIS 3.x. cf https://docs.qgis.org/3.34/en/docs/pyqgis_developer_cookbook/settings.html

example from linked document:

from qgis.core import QgsSettings
def store():
   s = QgsSettings()
   s.setValue("myplugin/mytext", "hello world")
   s.setValue("myplugin/myint",  10)
   s.setValue("myplugin/myreal", 3.14)


def read():
   s = QgsSettings()
   mytext = s.value("myplugin/mytext", "default text")
   myint  = s.value("myplugin/myint", 123)
   myreal = s.value("myplugin/myreal", 2.71)
   nonexistent = s.value("myplugin/nonexistent", None)

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.