4. PyQT and Interacting With QGIS
[qgis
gis
python
]
So far, so good. We’re now able to install our freshly created plugin to the local QGIS installation using one simple command. We can also enable the plugin in QGIS and see that it was activated.
First of all, we need to design an user interface. This is best done using QT Designer. Let’s go ahead and load the only *.ui
file in the repository, my was named geoapify_plugin_dockwidget_base.ui
:
The QDockWidget with dummy text is what the Plugin Builder plugin generates by default when you choose the Tool button with dock widget template. If you’re not sure what Plugin Builder is, check out my previous article.
Let’s reiterate on what the plugin should be able to do:
We will develop a simple QGIS toolbar to create a point vector layer using a text-based location search.1
Since the Geoapify API needs an API key, we’ve got to ask the user for it somehow. In our dock widget, we’re adding a text prompt for the address, and also a simple table to show the search results. After double-clicking on a specific row, the search result will be added to the main QGIS canvas.
Here is what I’ve been able to sketch up after a few minutes:
The first dock widget draft. Make sure to name your components appropriately - we will refer to components in our code using the objectName attribute.
Now, let’s circle back to the first part of this series1 - checking in on our features using issue tracking on GitLab. When creating a commit message, we’ll tag the issue number along with the keyword closes
, something like this:
The closes #1
is what Gitlab calls a closing pattern. What this means in practice is that once the feature branch is merged into main
, the issue gets automatically closed.
Now is the time to connect our UI with the Python code. Copy-pasting what we had in QT Designer, let’s modify geoapify_plugin_dockwidget.py
:
If you’re not familiar with QT, the key thing to know is how it lets us hook up functionality based on what the user does. QT allows us to respond to user actions through signals. When a signal is sent out, all the listeners get notified about a new event, e.g. when a user clicks a button.
That’s what our code does: whenever a click signal is triggered for the searchButton
component, it triggers the execution of the __handleSearchButtonClick
function – precisely what we need! In our simplistic implementation, a new message is pushed to QGIS, appearing in the message bar. Subsequent enhancements will involve sending a request to Geoapify containing the user prompt.
Let’s try the plugin logic in action:
In the next part, we will focus on connecting our Geoapify client with the plugin logic.