26- A Guide to Creating QGIS Plugins in a Step-by-Step Manner

If you’re looking to create QGIS plugins, then you’ve come to the right place! In this step-by-step guide, we’ll show you how to create your own plugin by creating essential files such as metadata.txt and mainPlugin.py, as well as how to use Python to …


This content originally appeared on DEV Community and was authored by Dr. Azad Rasul

If you're looking to create QGIS plugins, then you've come to the right place! In this step-by-step guide, we'll show you how to create your own plugin by creating essential files such as metadata.txt and mainPlugin.py, as well as how to use Python to create the init.py file. We'll also walk you through how to install your plugin and provide a tip on using the Plugin Builder to create a template for your plugin. Follow these instructions to create your own QGIS plugin and take your GIS analysis to the next level!

The first requirement for plugins is metadata.txt.

Use a text editor to create a metadata file:

[general]
name=TestPlugin
email=azad.rasul@soran.edu.iq
author=Azad Rasul
qgisMinimumVersion=3.0
description=This is an example plugin for greeting the world.
version=version 0.1

Save it as a "metadata" text document.

The second file is called "__init__.py" which includes the classFactory() method.

You can use Python to create a "__init__.py" file

from .mainPlugin import TestPlugin
def classFactory(iface):
  return TestPlugin(iface)

The third necessary file contains the main logic of the plugin. It must have initGui(), unload() and run() methods. We name it "mainPlugin.py" file that includes:

import os
import inspect
from PyQt5.QtWidgets import QAction
from PyQt5.QtGui import QIcon

# get the directory containing this script
cmd_folder = os.path.split(inspect.getfile(inspect.currentframe()))[0]

# define the TestPlugin class
class TestPlugin:

  def __init__(self, iface):
    self.iface = iface

  # initialize the plugin interface
  def initGui(self):
    # get the path to the plugin icon
    icon = os.path.join(os.path.join(cmd_folder, 'logo.png'))

    # create a new QAction with the plugin icon and label
    self.action = QAction(QIcon(icon), 'TestPlugin', self.iface.mainWindow())

    # connect the action to the run() method
    self.action.triggered.connect(self.run)

    # add the action to the toolbar and menu
    self.iface.addToolBarIcon(self.action)
    self.iface.addPluginToMenu("&TestPlugin", self.action)

  # unload the plugin
  def unload(self):
    # remove the action from the toolbar and menu
    self.iface.removePluginMenu("&TestPlugin", self.action)
    self.iface.removeToolBarIcon(self.action)

    # delete the action object
    del self.action

  # run the plugin
  def run(self):
    # display a message in the QGIS message bar
    self.iface.messageBar().pushMessage('Hello from TestPlugin!')

Save these three files and a logo in one folder and name it "TestPlugin" or the name of the plugin.

Then, copy the "TestPlugin" folder and paste it into QGIS 3 Plugins directory. On Windows, it is typically located at:

C:\Users\username\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins

Restart QGIS software, and you will be able to see your "TestPlugin" plugin in the list of installed plugins. By clicking on the TestPlugin icon, you can view the "Hello from TestPlugin!" text.

Alternatively, you can use the "Plugin Builder" to create a template plugin and then modify it to suit your needs.

In conclusion, creating QGIS plugins may seem daunting, but this step-by-step guide can help anyone create their customized plugin. By following the instructions provided, users can create necessary files such as metadata.txt and mainPlugin.py, and use Python to create the init.py file. The article also provides a tip on using the Plugin Builder to create a template for the plugin. With the plugin created and installed, users can take their GIS analysis to the next level.

The credit for my tutorials goes to Anita Graser and Ujaval Gandhi.

External resources:

1- PyQGIS Developer Cookbook

2- PyQGIS 101: Introduction to QGIS Python programming for non-programmers

3- Customizing QGIS with Python (Full Course Material)

4- PyQGIS samples

If you like the content, please SUBSCRIBE to my channel for the future content


This content originally appeared on DEV Community and was authored by Dr. Azad Rasul


Print Share Comment Cite Upload Translate Updates
APA

Dr. Azad Rasul | Sciencx (2023-03-27T23:01:37+00:00) 26- A Guide to Creating QGIS Plugins in a Step-by-Step Manner. Retrieved from https://www.scien.cx/2023/03/27/26-a-guide-to-creating-qgis-plugins-in-a-step-by-step-manner/

MLA
" » 26- A Guide to Creating QGIS Plugins in a Step-by-Step Manner." Dr. Azad Rasul | Sciencx - Monday March 27, 2023, https://www.scien.cx/2023/03/27/26-a-guide-to-creating-qgis-plugins-in-a-step-by-step-manner/
HARVARD
Dr. Azad Rasul | Sciencx Monday March 27, 2023 » 26- A Guide to Creating QGIS Plugins in a Step-by-Step Manner., viewed ,<https://www.scien.cx/2023/03/27/26-a-guide-to-creating-qgis-plugins-in-a-step-by-step-manner/>
VANCOUVER
Dr. Azad Rasul | Sciencx - » 26- A Guide to Creating QGIS Plugins in a Step-by-Step Manner. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/03/27/26-a-guide-to-creating-qgis-plugins-in-a-step-by-step-manner/
CHICAGO
" » 26- A Guide to Creating QGIS Plugins in a Step-by-Step Manner." Dr. Azad Rasul | Sciencx - Accessed . https://www.scien.cx/2023/03/27/26-a-guide-to-creating-qgis-plugins-in-a-step-by-step-manner/
IEEE
" » 26- A Guide to Creating QGIS Plugins in a Step-by-Step Manner." Dr. Azad Rasul | Sciencx [Online]. Available: https://www.scien.cx/2023/03/27/26-a-guide-to-creating-qgis-plugins-in-a-step-by-step-manner/. [Accessed: ]
rf:citation
» 26- A Guide to Creating QGIS Plugins in a Step-by-Step Manner | Dr. Azad Rasul | Sciencx | https://www.scien.cx/2023/03/27/26-a-guide-to-creating-qgis-plugins-in-a-step-by-step-manner/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.