重要

Translation is a community effort you can join. This page is currently translated at 85.71%.

12. 設定の読み込みと保存

ヒント

pyqgisコンソールを使わない場合、このページにあるコードスニペットは次のインポートが必要です:

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

多くの場合、プラグインでいくつかの変数が保存されて、そのプラグインを次に実行したときにユーザーがそれらの変数を入力したり選択したりする必要がないようにすると便利です。

これらの変数は保存され、QtとQGIS APIの助けを借りて取得できます。各変数について、変数にアクセスするために使用されるキーを選択する必要があります---ユーザの好みの色のためにキー「favourite_color」またはその他の意味のある文字列を使用できます。キーの名前をつけるときは何らかの構造を持たせることをお勧めします。

セッティングにはいくつかの種類があります:

  • グローバル設定 --- 特定のマシンのユーザーにバインドされます。QGIS自身、多くのグローバル設定を格納しており、例えば、メインウィンドウのサイズやデフォルトのスナップ許容範囲などです。設定は QgsSettings クラスを使って、例えば setValue()value() メソッドで処理されます。

    ここでは、これらの方法がどのように使われるかの例を見ることができます。

     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)
    

    value() メソッドの2番目のパラメータはオプションで、渡された設定名に対して以前の値が設定されていない場合に返されるデフォルト値を指定します。

    For a method to pre-configure the default values of the global settings through the qgis_global_settings.ini file, see 組織内でのQGISの導入 for further details.

  • プロジェクト設定 --- プロジェクト間で異なるため、プロジェクトファイルと関連付けられます。マップキャンバスの背景色や、目的の座標参照系 (CRS) などはその一例です。あるプロジェクトでは白い背景とWGS84が適しているかもしれませんが、別のプロジェクトでは黄色い背景とUTM投影法が適しているかもしれません。

    使用例を以下に示します。

     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)
    

    ご覧の通り、writeEntry()) メソッドは多くのデータ型(整数、文字列、リスト)で使用されますが、設定値を読み出すためのメソッドは複数存在し、データ型ごとに対応するメソッドを選択する必要があります。

  • マップレイヤ設定 --- これらの設定は、あるプロジェクトのマップレイヤの特定のインスタンスに関連しています。そのため、1つのシェープファイルで2つのマップレイヤインスタンスを作成した場合、それらの設定は共有されません。設定はプロジェクトファイル内に保存されるため、ユーザーがプロジェクトを再度開くと、レイヤ関連の設定は再びそこに存在します。設定値は customProperty() メソッドで取得し、 setCustomProperty() メソッドで設定します。

    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")