7

I'm designing a QGIS plugin where I'd like to offer the user the functionality to select the extent of a bounding box from the canvas like in some tools like Create grid.

enter image description here I've explored QT Designer but there is no widget with this functionality.

I have also explored the tool code of tools that offer this functionality but found nothing.

Finally, I didn't find any documentation about this feature. Any ideas?

5
  • 1
    I think it is a Qt contextual menu on a QPushButton ... with an action that retrieves the QgsMapCanvas.extent() (pyQGIS API). Commented Jan 10, 2020 at 15:33
  • 1
    You could check how it is used from the ExtentSelectionPanel.py file.
    – Joseph
    Commented Jan 10, 2020 at 15:33
  • I was looking for a similar script! I'll explore it on Monday, along with the API documentation. I'll get back to you, have a good weekend! Commented Jan 10, 2020 at 16:08
  • I solved my problem ! Thanks for your answers. Commented Jan 14, 2020 at 14:51
  • 1
    @VincentBré If you have solved your question, it would still be helpful to post the answer for others who end up here
    – jeremyg19
    Commented May 18, 2022 at 14:26

1 Answer 1

3

You mean select a region on the map, on the Canvas map? And that the selected region is shown on the map by clicking and dragging the cursor? From a dialog box or button? And capture the coordinates of the region for a process?

In that case, you must create a custom map tool that interacts with the mapCanvas. That is, write a class that extends QgsMapTool or QgsMapToolEmitPoint.

In the cookbook they explain in detail how to do it, here https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/canvas.html#id4 Go to the item Writing Custom Map Tools

The map tools require the mapCanvas on which they will act. Inside the class you must rewrite the different events, for example

def canvasPressEvent(self, event):

The event gives you the coordinates where you clicked event.pos().x() in screen coordinates, but you can convert them to map coordinates if you previously, in the init, create a QgsCoordinateTransform, for example with:

self.transform = self.can.getCoordinateTransform()

Concerning the trace of the rectangle drawn by the user, you use a QgsRubberBand that requires the mapCanvas and the geometry it will represent.

To activate the tool after clicking a button, simply create an instance of your tool (QgsMapTool), tool=youTool(canvas) and then activate it by assigning it to the canvas with iface.mapCanvas().setMapTool(youTool).

There are several things to consider but this will guide you if you are going to create your own mapTool for what you want.

1
  • I have already solved my problem. Thanks for your answer and your explanations. Commented Sep 2, 2020 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.