Docs in progress for 'QGIS testing'. Visit https://docs.qgis.org/3.4 for QGIS 3.4 docs and translations.
如果您在Pyqgis控制台之外,则此页上的代码段需要以下导入:
from qgis.core import (
QgsProject,
QgsSettings,
QgsVectorLayer
)
警告
Despite our constant efforts, information beyond this line may not be updated for QGIS 3. Refer to https://qgis.org/pyqgis/master for the python API documentation or, give a hand to update the chapters you know about. Thanks.
很多时候,插件保存一些变量是有用的,这样用户就不必在下次运行插件时再次输入或选择这些变量。
这些变量可以在qt和qgis api的帮助下保存和检索。对于每个变量,您应该选择一个用于访问变量的键---对于用户最喜欢的颜色,您可以使用键“最喜欢的颜色”或任何其他有意义的字符串。建议为键的命名提供一些结构。
我们可以区分几种设置:
global settings --- they are bound to the user at a particular machine.
QGIS itself stores a lot of global settings, for example, main window size or
default snapping tolerance. Settings are handled using the QgsSettings
class.
The setValue()
and value()
methods from this class provide
这里您可以看到如何使用这些方法的示例。
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 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.
下面是用法示例。
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) As you can see, the :meth:`writeEntry() <qgis.core.QgsProject.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.
customProperty()
method, and can be set using the setCustomProperty()
one.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")