PyQGIS plug-in development experience

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/this_is_id/article/details/90020197

Environment Configuration

method one

Official documents are described: https://docs.qgis.org/3.4/en/docs/pyqgis_developer_cookbook/intro.html#running-custom-applications

Method Two

If it pycharm development, environment configuration is relatively simple, direct addition of C: \ Program Files \ QGIS 3.4 \ bin \ python-qgis.bat (ltr version python-qgis-ltr.bat), as shown below:

Writing Plugins

New Construction

My Project: D: \ pycharm \ workspace \ qgis_plugin_test

Plug-in architecture

The official document: https://docs.qgis.org/3.4/en/docs/pyqgis_developer_cookbook/plugins/plugins.html#plugin-files

My plug-in architecture, as follows:

 Structure Description

__init__.py

Plug-entry, class names must be classFactory

# -*- coding: utf-8 -*-

def classFactory(iface):
    from .main_plugin import PluginTest
    return PluginTest(iface)

main_plugin.py

It must include two functions

  • initGui: for loading plug-ins
  • unload: used to uninstall the plug

My main_plugin.py as follows:

# -*- coding: utf-8 -*-
# @Time    : 2018/9/30 9:32
# @Author  : llc
# @File    : main_plugin.py


from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QAction, QDialog

import os


class PluginTest:

    def __init__(self, iface):
        self.iface = iface
        self.title = '这是一个插件'

    def initGui(self):
        '''插件加载'''
        self.action = QAction(QIcon(os.path.dirname(__file__)+"/icon.png"), self.title, self.iface.mainWindow())
        self.action.setWhatsThis(self.title)
        self.action.triggered.connect(self.run)

        # 添加到菜单栏
        self.iface.addPluginToMenu("插件测试", self.action)
        # 添加到工具栏
        self.iface.addToolBarIcon(self.action)

    def unload(self):
        '''插件卸载'''
        # 从菜单栏删除
        self.iface.removePluginMenu("插件测试", self.action)
        # 从工具栏删除
        self.iface.removeToolBarIcon(self.action)

    def run(self):
        dialog = QDialog(self.iface.mainWindow())
        dialog.setWindowTitle(self.title)
        dialog.exec_()

icon

Plug-in icon

metadata.txt

Plug-in metadata official document: https://docs.qgis.org/3.4/en/docs/pyqgis_developer_cookbook/plugins/plugins.html#plugin-metadata

My metadata.txt:

[general]
name=这是一个插件
description=再说一遍,这是一个插件

version=0.0.0
qgisMinimumVersion=3.0
author=llc
icon=icon.png

changelog=
    - 没有日志

Plug-in debugging

The official document also described (ultra trouble, no trial is successful): https://docs.qgis.org/3.4/en/docs/pyqgis_developer_cookbook/plugins/ide_debugging.html

My method:

  1. The project directory is linked to QGIS plugin installation directory, open cmd , execute: cd C: \ the Users \ your user name \ AppData \ Roaming \ QGIS \ QGIS3 \ profiles \ default \ python \ plugins
  2. mklink /J qgis_plugin_test D:\pycharm\workspace\qgis_plugin_test
  3. Then there will be a similar plug-in directory shortcut things:
  4. Open the QGIS, into the plug-in management, find this is a plug-in , for the first time need to run marked
  5. This is a plug-in menus and toolbars will be more of an icon:
  6. Click the button to pop up the dialog box:
  7. If you need to debug, such as the code self.title changed again, this is a plug-in , then go to step 4 to cancel and re-check
  8. Then click on the plug-in button, the title will be updated:

release

The plug-zip the package can be released, accompanied by zip package Download:  https://download.csdn.net/download/this_is_id/11168216

to sum up

  • If the code is not used QGIS unit associated libraries, for example, only interface debugging, debugging can be done in pycharm in, and then run the plug QIGS
  • Less QGIS plugin development documentation from the module can be viewed at the official: https://qgis.org/pyqgis/3.4/

Guess you like

Origin blog.csdn.net/this_is_id/article/details/90020197