12. Citirea și stocarea setărilor

Sugestie

The code snippets on this page need the following imports if you’re outside the pyqgis console:

1from qgis.core import (
2  QgsProject,
3  QgsSettings,
4  QgsVectorLayer
5)

De multe ori, pentru un plugin, este utilă salvarea unor variabile, astfel încât utilizatorul să nu trebuiască să le reintroducă sau să le reselecteze, la fiecare rulare a plugin-ului.

Aceste variabile pot fi salvate cu ajutorul Qt și QGIS API. Pentru fiecare variabilă ar trebui să alegeți o cheie care va fi folosită pentru a accesa variabila — pentru culoarea preferată a utilizatorului ați putea folosi o cheie de genul „culoare_favorită” sau orice alt șir semnificativ. Este recomandabil să folosiți o oarecare logică în denumirea cheilor.

We can differentiate between several types of settings:

  • global settings — they are bound to the user at a particular machine. QGIS itself stores a lot of global settings, for example, main window size or default snapping tolerance. Settings are handled using the QgsSettings class, through for example the setValue() and value() methods.

    Here you can see an example of how these methods are used.

     1def store():
     2  s = QgsSettings()
     3  s.setValue("myplugin/mytext", "hello world")
     4  s.setValue("myplugin/myint",  10)
     5  s.setValue("myplugin/myreal", 3.14)
     6
     7def read():
     8  s = QgsSettings()
     9  mytext = s.value("myplugin/mytext", "default text")
    10  myint  = s.value("myplugin/myint", 123)
    11  myreal = s.value("myplugin/myreal", 2.71)
    12  nonexistent = s.value("myplugin/nonexistent", None)
    13  print(mytext)
    14  print(myint)
    15  print(myreal)
    16  print(nonexistent)
    

    The second parameter of the value() method is optional and specifies the default value that is returned if there is no previous value set for the passed setting name.

    For a method to pre-configure the default values of the global settings through the global_settings.ini file, see Deploying QGIS within an organization for further details.

  • setările proiectului — variază între diferite proiecte și, prin urmare, ele sunt conectate cu un fișier de proiect. Culoarea de fundal a suportului hărții sau sistemul de coordonate de referință (CRS), de exemplu — fundal alb și WGS84 ar putea fi potrivite pentru un anumit proiect, în timp ce fondul galben și proiecția UTM ar putea fi mai bune pentru altul.

    An example of usage follows.

     1proj = QgsProject.instance()
     2
     3# store values
     4proj.writeEntry("myplugin", "mytext", "hello world")
     5proj.writeEntry("myplugin", "myint", 10)
     6proj.writeEntryDouble("myplugin", "mydouble", 0.01)
     7proj.writeEntryBool("myplugin", "mybool", True)
     8
     9# read values (returns a tuple with the value, and a status boolean
    10# which communicates whether the value retrieved could be converted to
    11# its type, in these cases a string, an integer, a double and a boolean
    12# respectively)
    13
    14mytext, type_conversion_ok = proj.readEntry("myplugin",
    15                                            "mytext",
    16                                            "default text")
    17myint, type_conversion_ok = proj.readNumEntry("myplugin",
    18                                              "myint",
    19                                              123)
    20mydouble, type_conversion_ok = proj.readDoubleEntry("myplugin",
    21                                                    "mydouble",
    22                                                    123)
    23mybool, type_conversion_ok = proj.readBoolEntry("myplugin",
    24                                                "mybool",
    25                                                123)
    

    As you can see, the writeEntry() method is used for many data types (integer, string, list), but several methods exist for reading the setting value back, and the corresponding one has to be selected for each data type.

  • map layer settings — these settings are related to a particular instance of a map layer with a project. They are not connected with underlying data source of a layer, so if you create two map layer instances of one shapefile, they will not share the settings. The settings are stored inside the project file, so if the user opens the project again, the layer-related settings will be there again. The value for a given setting is retrieved using the customProperty() method, and can be set using the setCustomProperty() one.

    1vlayer = QgsVectorLayer()
    2# save a value
    3vlayer.setCustomProperty("mytext", "hello world")
    4
    5# read the value again (returning "default text" if not found)
    6mytext = vlayer.customProperty("mytext", "default text")