天天看點

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提供的主題建構。