Graphics Reference
In-Depth Information
Let's add one more feature to our test plugin: a toolbar item, which, when clicked on, also
calls the
onRun()
method. Find a suitable PNG format image that is 24 x 24 pixels (the
default size for a QGIS toolbar icon), and save that image into your plugin's directory un-
der the name
icon.png
. Then, change your
initGui()
method to look like the fol-
lowing:
def initGui(self):
icon = QIcon(":/plugins/testPlugin/icon.png")
self.action = QAction(icon, "Run",
self.iface.mainWindow())
QObject.connect(self.action, SIGNAL("triggered()"),
self.onRun)
self.iface.addPluginToMenu("Test Plugin", self.action)
self.iface.addToolBarIcon(self.action)
The changed lines have been highlighted. As you can see, we've added an icon to our
QAction
object, and then also called the
addToolBarIcon()
method to add our ac-
tion to the Plugins toolbar.
We'll also have to add one extra line to our
unload()
method to remove the toolbar icon
when the plugin is unloaded: