Geography Reference
In-Depth Information
First we need to create what's called an action. This is a Qt object of
type QAction . It's used to define an action that will later be used on a
menu or a toolbar. On line 19, we create our action by supplying three
arguments:
• The icon for the toolbar. This is a combination of the prefix
( /plugins/zoom_to_point ) and the icon file name ( icon.png ) as spec-
ified in our resources file.
• Some text that's used in the menu and tooltip, in this case “Zoom
To Point plugin.”
• A reference to the parent for the plugin, in this case the main
window of QGIS.
Once we have created the action, we set some descriptive text to be
used with the WhatsThis function (that's the little arrow you find on a lot
of applications that you click to activate and then click a toolbar item
or other GUI element to get a description). On line 21, we do one last
thing with the action to connect it to the run ( ) method. This basically
connects things so that when the OK button on the dialog box is clicked
the run ( ) method in our ZoomToPoint class is called.
Next we need to actually put our nicely configured action on the menu
and toolbar in the GUI. The QgisInterface class that we played with in
the Python console contains the methods we need. On line 24, we use
addToolBarIcon ( ) to add the icon for our tool to the plugin toolbar in
QGIS. To add it to the menu, we use addPluginMenu ( ) method, as shown
on line 25. Now our GUI is set up and ready to use.
The unload ( ) method is pretty simple. It uses the removePluginMenu ( )
and removeToolBarIcon ( ) methods to remove the menu item and the icon
from the toolbar. Remember this method is called only when you unload
the plugin from QGIS using the Plugin Manager.
Finally, we are ready to add the bit of code that does the real work.
Like most GUI applications, the bulk of the code has to do with the
user interface while a few bytes do the actual work. In our case, the
actual work is done by the run ( ) method that is defined on line 1 in the
following listing:
Download zoomtopoint.py
def run(self):
Line 1
# create and show the ZoomToPoint dialog
-
print "Creating ZoomToPoint Dialog"
-
dlg = ZoomToPointDialog()
-
 
Search WWH ::




Custom Search