LoginSignup
1
4

More than 3 years have passed since last update.

QGISプラグインの設定情報を保存

Posted at

はじめに

QGISでプラグインの設定情報を保持したい場合があります。
そして、プロジェクトファイルごとに設定を変更したいことがありました。
公式ドキュメントを見て使い方の把握

QGIS本体に情報を保存する場合


setting = QSetting()
key = 'text'

# 書き込み
settings.setValue(key, 'hello')
# 読み込み
value = settings.value(key, defaultValue='hoge')
print(value)

QGISプロジェクトに情報を保存する場合


# -*- coding: utf-8 -*-
proj = QgsProject.instance()

# 書き込み
proj.writeEntry('testplugin', 'text', 'hello world')
proj.writeEntry('testplugin', 'bool', True)
proj.writeEntry('testplugin', 'num', 127)
proj.writeEntry('testplugin', 'double', 127.1)
# listはテキストのみ
proj.writeEntry('testplugin', 'list', ['a',  'b'])

# 読み込み
# 返り値は、格納されている値とNullかどうか
t, _ = proj.readEntry('testplugin', 'text')
b, _ = proj.readBoolEntry('testplugin', 'bool')
n, _ = proj.readNumEntry('testplugin', 'num')
d, _ = proj.readDoubleEntry('testplugin', 'double')
l, _ = proj.readListEntry('testplugin', 'list')

print(t, b, n, d, l)
# 'hello world', True, 127, 127.0, ['a', 'b']

Layerに情報を保存する場合

Layerに保存した場合もプロジェクトに情報が保存されます


layer = iface.activeLayer()

# 書き込み
layer.setCustomProperty('text', 'hello')
# 読み込み
text = layer.customProperty('text' ,defaultValue='hoge')
print(text)

参考

QGIS API Documentation: QgsProject
PyQGIS Cookbook 3.4
PyQGIS Cookbook 2.18

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4