Docs in progress for 'QGIS testing'. Visit https://docs.qgis.org/3.4 for QGIS 3.4 docs and translations.
根据您将要开发的插件类型,将其功能作为处理算法(或一组算法)添加可能是更好的选择。这将在QGIS中提供更好的集成、附加功能(因为它可以在处理组件中运行,例如建模器或批处理接口),以及更快的开发时间(因为处理将占用大部分工作)。
要分发这些算法,您应该创建一个新的插件,将它们添加到处理工具箱中。该插件应包含一个算法提供程序,在实例化插件时必须注册该算法提供程序。
要从头开始创建包含算法提供程序的插件,可以使用插件生成器执行以下步骤:
如果要将现有插件添加到处理中,则需要添加一些代码。
在你 metadata.txt
,您需要添加一个变量:
hasProcessingProvider=yes
在python文件中,用 initGui
方法,您需要调整如下行:
from qgis.core import QgsApplication
from .processing_provider import Provider
class YourPluginName():
def __init__(self):
self.provider = None
def initProcessing(self):
self.provider = Provider()
QgsApplication.processingRegistry().addProvider(self.provider)
def initGui(self):
self.initProcessing()
def unload(self):
QgsApplication.processingRegistry().removeProvider(self.provider)
可以创建文件夹 processing_provider
其中包含三个文件:
__init__.py
with nothing in it. This is necessary to make a valid Python package.provider.py
which will create the Processing provider and expose your algorithms.from qgis.core import QgsProcessingProvider
from .example_processing_algorithm import ExampleProcessingAlgorithm
class Provider(QgsProcessingProvider):
def loadAlgorithms(self, *args, **kwargs):
self.addAlgorithm(ExampleProcessingAlgorithm())
# add additional algorithms here
# self.addAlgorithm(MyOtherAlgorithm())
def id(self, *args, **kwargs):
"""The ID of your plugin, used for identifying the provider.
This string should be a unique, short, character only string,
eg "qgis" or "gdal". This string should not be localised.
"""
return 'yourplugin'
def name(self, *args, **kwargs):
"""The human friendly name of your plugin in Processing.
This string should be as short as possible (e.g. "Lastools", not
"Lastools version 1.0.1 64-bit") and localised.
"""
return self.tr('Your plugin')
def icon(self):
"""Should return a QIcon which is used for your provider inside
the Processing toolbox.
"""
return QgsProcessingProvider.icon(self)
example_processing_algorithm.py
which contains the example algorithm file.
Copy/paste the content of the script template:
https://github.com/qgis/QGIS/blob/master/python/plugins/processing/script/ScriptTemplate.py现在您可以在qgis中重新加载插件,并且应该在处理工具箱和建模器中看到示例脚本。