天天看点

PyQGisCookbook--读取和存储设置(十三)读取和存储设置

如果您不在pyqgis控制台,则需要导入以下模块:

from qgis.core import (
  QgsProject,
  QgsSettings,
  QgsVectorLayer
)
           

读取和存储设置

有时候保存一些插件变量非常有用,这样用户下次运行插件时就不必再次输入或选择它们。

这些变量可以借助Qt和QGIS API进行保存和检索。对于每个变量,您应该选择一个将用于访问该变量的键-例如用户喜欢的颜色,您可以使用键“ favourite_color”或任何其他有意义的字符串。建议给键命名时提供一些结构。

我们可以区分几种类型的设置:

  • 全局设置 -它们绑定到特定计算机上的用户。QGIS本身存储许多全局设置,例如,主窗口大小。设置是通过

    QgsSettings

    setValue()

    和 

    value()

    方法来使用的 。

    在这里,您可以看到有关如何使用这些方法的示例。

    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)
      print(mytext)
      print(myint)
      print(myreal)
      print(nonexistent)
               
    方法的第二个参数

    value()

     是可选的,它指定名称没有获得对应值情况下返回的默认值。
  • 项目(Project)设置 -不同项目之间有所不同,因此它们与项目文件连接。地图画布背景颜色或目标坐标参考系统(CRS)就是示例-白色背景和WGS84可能适合一个项目,而黄色背景和UTM投影更适合另一个项目。

    使用示例如下。

    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 (returns a tuple with the value, and a status boolean
    # which communicates whether the value retrieved could be converted to
    # its type, in these cases a string, an integer, a double and a boolean
    # respectively)
    
    mytext, type_conversion_ok = proj.readEntry("myplugin",
                                                "mytext",
                                                "default text")
    myint, type_conversion_ok = proj.readNumEntry("myplugin",
                                                  "myint",
                                                  123)
    mydouble, type_conversion_ok = proj.readDoubleEntry("myplugin",
                                                        "mydouble",
                                                        123)
    mybool, type_conversion_ok = proj.readBoolEntry("myplugin",
                                                    "mybool",
                                                    123)
               
    如您所见,该

    writeEntry()

     方法适用于所有数据类型,但是必须为每种数据类型选择相应的读回设置值方法。
  • 地图图层设置 -这些设置与项目的特定地图图层相关。它们未与图层的基础数据源连接,因此,如果您创建一个shapefile的两个地图图层实例,则它们将不会共享设置。这些设置存储在项目文件中,因此,如果用户再次打开项目,则与图层相关的设置将再次出现。使用

    customProperty()

    方法可以检索给定设置的值,也可以使用

    setCustomProperty()

    方法进行设置 。
    vlayer = QgsVectorLayer()
    # save a value
    vlayer.setCustomProperty("mytext", "hello world")
    
    # read the value again (returning "default text" if not found)
    mytext = vlayer.customProperty("mytext", "default text")
               

下一个   前一个

©版权所有2002-现在,QGIS项目 最近更新于2020年4月3日09:14。

使用Sphinx使用Read the Docs提供的主题构建。