Outdated version of the documentation. Find the latest one here.

설정을 읽고 저장하기

많은 경우 플러그인에 몇몇 변수들을 저장해서 다음에 플러그인을 실행할 때 사용자가 변수를 입력하거나 선택할 필요가 없도록 하는 것이 유용합니다.

Qt 및 QGIS의 API를 통해 이 변수들을 저장하고 불러올 수 있습니다. 각 변수에 대해 해당 변수에 접근하는 데 사용될 키(key)를 선택해야 합니다. 사용자가 선호하는 색상의 경우 “favourite_color” 또는 다른 어떤 의미가 있는 문자열이라도 키로 사용할 수 있습니다. 키를 명명할 때 일종의 구조를 부여하는 것이 좋습니다.

We can make difference between several types of settings:

  • global settings — they are bound to the user at particular machine. QGIS itself stores a lot of global settings, for example, main window size or default snapping tolerance. This functionality is provided directly by Qt framework by the means of QSettings class. By default, this class stores settings in system’s “native” way of storing settings, that is — registry (on Windows), .plist file (on macOS) or .ini file (on Unix). The QSettings documentation is comprehensive, so we will provide just a simple example

    def store():
      s = QSettings()
      s.setValue("myplugin/mytext", "hello world")
      s.setValue("myplugin/myint",  10)
      s.setValue("myplugin/myreal", 3.14)
    def read():
      s = QSettings()
      mytext = s.value("myplugin/mytext", "default text")
      myint  = s.value("myplugin/myint", 123)
      myreal = s.value("myplugin/myreal", 2.71)

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

  • project settings — vary between different projects and therefore they are connected with a project file. Map canvas background color or destination coordinate reference system (CRS) are examples — white background and WGS84 might be suitable for one project, while yellow background and UTM projection are better for another one. An example of usage follows

    proj = QgsProject.instance()
    # store values
    proj.writeEntry("myplugin", "mytext", "hello world")
    proj.writeEntry("myplugin", "myint", 10)
    proj.writeEntry("myplugin", "mydouble", 0.01)
    proj.writeEntry("myplugin", "mybool", True)
    # read values
    mytext = proj.readEntry("myplugin", "mytext", "default text")[0]
    myint = proj.readNumEntry("myplugin", "myint", 123)[0]

    As you can see, the writeEntry() method is used for all data types, 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 in project file, so if the user opens the project again, the layer-related settings will be there again. This functionality has been added in QGIS v1.4. The API is similar to QSettings — it takes and returns QVariant instances

    # save a value
    layer.setCustomProperty("mytext", "hello world")
    # read the value again
    mytext = layer.customProperty("mytext", "default text")