Graphics Reference
In-Depth Information
def onClick(self):
QMessageBox.information(self.iface.mainWindow(),
"debug",
"Click")
Apart from a few extra import statements (which we'll need later on), this is almost
identical to our earlier example plugin. The onClick() method, of course, is just a
placeholder so we can tell if the plugin is working.
We can now run our plugin by typing make deploy in the command line, starting up
QGIS, and enabling the plugin using the Manage and Install Plugins... command, just
like we did earlier. If all goes well, the plugin's icon should appear in the QGIS toolbar,
and when you select it, the "Click" message should be displayed.
Next, we want to make our toolbar icon checkable . That is, when the user clicks on our
icon, we want to highlight it, activate our map tool, and keep the icon highlighted until the
user either clicks on the icon again or switches to a different tool. To make the toolbar
icon checkable, add the following line to your initGui() method, immediately after
the self.action = QAction(...) statement:
self.action.setCheckable(True)
We then have to respond to the checking and unchecking of our toolbar icon by activating
and deactivating our map tool. Here is what the code will look like:
def onClick(self):
if not self.action.isChecked():
# ...deactivate map tool...
return
self.action.setChecked(True)
# ...activate map tool...
The first thing we do is see if the user has unchecked our icon, and if this is the case, we
deactivate the map tool. Otherwise, we visually highlight the icon by calling
self.action.setChecked(True) , and then activate our map tool. In this way, our
plugin will act like a mode within QGIS; clicking on the icon will activate the map tool,
and clicking on it again (or selecting a different icon) will deactivate it.
Search WWH ::




Custom Search