Setting and Coding Your Basic QGIS Plugin - Hello World
Setting up and Coding your Plugin –Hello World
In this tutorial we proceed to tweak the default user
interface of the plugin we just created and create button that will then
display an Info message of Hello World
on the QGIS User Interface.
Prerequisites
In this tutorial we needed the following
Software
QGIS Software required
(I use version: 2.12.1 Lyon or any other versions current is QGIS 3.8 Noosa )
Click the link to download the software appropriate to your
platform (https://qgis.org/en/site/forusers/download.html)
the software is open source
A code
Editor
Notepad++ (it’s
lightweight and open source) click on the link https://notepad-plus-plus.org/ to
download the latest version
IDE
Qt Creator IDE
(recommended as I prefer to use it over Notepad++) as they come with GUI
(graphical user interface ) toolkit for user interface creation (click https://www.qt.io/download) and use the
Open Source Version to download
Platform
Operating System : In this tutorial Windows 7
Steps on Creating Your Custom Quantum (QGIS) Plugin
1. Click on Start Menu ->Click Qt Creator IDE. We use the Open Source Version for this
tutorial
2. Go to File Menu and go to Open File or Project .Go to the
directory where your plugin is placed on the HelloWorld folder and select the
QT Ui File with the suffix
dockwidget_base (in this example hello_world_dockwidget_base).
3. As you see
this is the very same plugin displayed in the Part 1 of this tutorial. Now we
modify it.
For this tutorial we display a warning or information
bar on the top of the QGIS software using Python.
4. We then drag a button (Push Button) on the left to the plugin panel and change its text Property on the right to “Display Hello World”
5.Now on the File
Menu we click on Open File or
Project .We choose the hello_world Python file.
The hello_world module python file contains the logic
and code for the plugin. So this is where we will code the Information Display.
6.The IDE code editor then opens. We go to the run module .the run module is where we
put the Info “Hello World” because it is executed after the QGIS software has
finished initializing its class and its GUI.
7. Now we edit the code just below the
self.dockwidget.show()
def run(self):
"""Run
method
that
loads
and
starts
the
plugin"""
if not self.pluginIsActive:
self.pluginIsActive = True
#print "** STARTING HelloWorld"
# dockwidget may not exist if:
# first run of plugin
# removed on close (see self.onClosePlugin method)
if self.dockwidget
== None:
# Create the dockwidget (after translation) and keep reference
self.dockwidget
= HelloWorldDockWidget()
# connect to provide cleanup on closing of dockwidget
self.dockwidget.closingPlugin.connect(self.onClosePlugin)
# show the dockwidget
# TODO: fix to allow choice of dock location
self.iface.addDockWidget(Qt.LeftDockWidgetArea, self.dockwidget)
self.dockwidget.show()
self.dockwidget.pushButton.clicked.connect(self.Display)
def Display(self):
self.iface.messageBar().pushMessage("Hello World", "Welcome to QGIS Python Programming",QgsMessageBar.INFO)
So what would be the result be?
After adding the code.
8. Restart QGIS. Click on the Plugins menu and click
on the Display Hello World Message Menu
Item to activate the plugin.
9. Now as you see the interface looks the same as what we have manipulated in the QT Creator IDE. Now click on the button Display Hello World.
Now as you see. We now have an
Information Message “Hello World , Welcome to QGIS Python Programming.” Now
there you have it. A basic Plugin . Congratulations on the Part 2 of our
Tutorial.
So let us dissect the code that I
have written
self.dockwidget.pushButton.clicked.connect(self.Display)
def Display(self):
self.iface.messageBar().pushMessage("Hello World", "Welcome to QGIS Python Programming",QgsMessageBar.INFO)
(1) self.dockwidget.pushButton.clicked.connect(self.Display)
This portion of
code when sets the event handler which is defined by the module Display after it whenever the user clicks the pushButton located on the dockwidget
which is the panel that holds all the controls including the label and the
button (pushButton)
(2)def Display(self):
This portion defines the logic by which the
MESSAGE BAR will display Hello World
(3)self.iface.messageBar().pushMessage("Hello World", "Welcome to QGIS
Python Programming",QgsMessageBar.INFO)
All
of it is written after the self.dockwidget.show() snippet because it is only
executed after the Widget/Plugin shows itself or the plugin has finished
loading its component controls.








Comments
Post a Comment