
Adding functionality using actions
Actions are a convenient way of adding custom functionality to QGIS. Actions are created for specific layers, for example, our populated places dataset, popp.shp
. Therefore, to create actions, we go to Layer Properties | Actions. There are different types of actions, such as the following:
- Generic actions start external processes; for example, you run command-line applications such as ogr2ogr
Note
ogr2ogr is a command-line tool that can be used to convert file formats and, at the same time, perform operations such as spatial or attribute selections and reprojecting.
- Python actions execute Python scripts
- Open actions open a file using your computer's configured default application, that is, your PDF viewing application for
.pdf
files or your browser for websites - Operating system (Mac, Windows, and Unix) actions work like generic actions but are restricted to the respective operating system
Configuring your first Python action
Click on the Add default actions button on the right-hand side of the dialog to add some example actions to your popp
layer. This is really handy to get started with actions. For example, the Python action called Selected field's value will display the specified attribute's value when we use the action tool. All that we need to do before we can give this action a try is update it so that it accesses a valid attribute of our layer. For example, we can make it display the popp
layer's TYPE
attribute value in a message box, as shown in the next screenshot:
- Select the Selected field's value action in Action list.
- Edit the Action code at the bottom of the dialog. You can manually enter the attribute name or select it from the drop-down list and click on Insert field.
- To save the changes, click on Update selected action:
To use this action, close the Layer Properties dialog and click on the drop-down arrow next to the Run Feature Action button. This will expand the list of available layer actions, as shown in the following screenshot:

Click on the Selected field's value entry and then click on a layer feature. This will open a pop-up dialog in which the action will output the feature's TYPE
value. Of course, we can also make this action output more information, for example, by extending it to this:
QtGui.QMessageBox.information(None, "Current field's value", "Type: [% "TYPE" %] \n[% "F_CODEDESC" %]")
This will display the TYPE
value on the first line and the F_CODEDESC
value on the second line.

Opening files using actions
To open files directly from within QGIS, we use the Open actions. If you added the default actions in the previous exercise, your layer will already have an Open file action. The action is as simple as [% "PATH" %]
for opening the file path specified in the layer's path attribute. Since none of our sample datasets contain a path attribute, we'll add one now to test this feature. Check out Chapter 3, Data Creation and Editing, if you need to know the details of how to add a new attribute. For example, the paths added in the following screenshot will open the default image viewer and PDF viewer application, respectively:

While the previous example uses absolute paths stored in the attributes, you can also use relative paths by changing the action code so that it completes the partial path stored in the attribute value; for example, you can use C:\temp\[% "TYPE" %].png
to open .png
files that are named according to the TYPE
attribute values.
Opening a web browser using actions
Another type of useful Open action is opening the web browser and accessing certain websites. For example, consider this action:
http://www.google.com/search?q=[% "TYPE" %]
It will open your default web browser and search for the TYPE
value using Google, and this action:.
https://en.wikipedia.org/w/index.php?search=[% "TYPE" %]
will search on Wikipedia.