Graphics Reference
In-Depth Information
When it starts up, QGIS looks through the various Python packages it finds in the
~/.qgis2/python/plugins directory. For each package, it attempts to call the top-
level function named ClassFactory() in the plugin's __init__.py file. This func-
tion should import and return an instance of the plugin's object, like this:
def ClassFactory(iface):
from myPlugin import MyPlugin
return MyPlugin(iface)
Tip
Obviously, you should change the name of myPlugin (and MyPlugin ) to something
more meaningful when you write a real plugin.
While it's usual to define the plugin in a separate module, you can create it directly within
the __init__.py module if you prefer. The important thing is to define a class that
provides the following methods:
__init__(iface) : This initializes the plugin object. Note that this should ac-
cept the iface variable passed to the class factory and store it in an instance
variable for later use.
initGui() : This initializes the plugin's user interface. This would typically in-
volve adding the plugin to the QGIS menus and toolbar, and setting up the signal
handlers to respond to various events.
unload() : This removes the plugin's user-interface elements. This would nor-
mally include removing the plugin from the QGIS menus and toolbar, and discon-
necting the signal handlers defined in the plugin's initGui() method.
The __init__(iface) method is called by your class factory function to initialize the
plugin object itself. The initGui() method is then called by QGIS when the program
starts up, or when the user installs the plugin. Finally, the unload() method is called by
QGIS when the user uninstalls the plugin or when QGIS shuts down.
A plugin doesn't usually run right away when QGIS starts up. Instead, it installs various
menu and toolbar items, which the user can then select to perform various actions. For ex-
ample, a simple plugin may have just one menu item and one toolbar item, and when the
user selects one of these, the plugin performs its one and only action. More sophisticated
plugins might have a range of menu and toolbar items, each one performing a different ac-
tion.
Search WWH ::




Custom Search