2

I implemented a maptool that draws a red cross on the point that is being right-clicked on. There are good how-tos, such as this topic : Programatically check for mouse click in PyQGIS?

But I happen to completely lose the left-click behaviour. This means that I cannot navigate through my layer with left click. I used a little trick ("iface.actionPan().trigger()") to get the pan action anyway, but it sets the mapTool to another object, an instance of mapToolPan. This later on causes various bugs, because I am calling methods that belong to my homemade PointTool class on a mapToolPan instance.

If the user right clicks on QGIS, the toolPan is set to my homemade PointTool class again.

Here is the code where I need help (I only implemented the canvasReleaseEvent because I only use this event) :

class PointTool(QgsMapTool):

def __init__(self, canvas):
    QgsMapTool.__init__(self, canvas)
    self.canvas = canvas

def canvasReleaseEvent(self, event):
    """ Checks which click was used : if left, triggers the pan action, else, adds point. """

    if event.button() == QtCore.Qt.RightButton:
        # The right click triggers the point selection
        x = event.pos().x()
        y = event.pos().y()
        self.clickedPoint = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)
        self.showPoint(self.clickedPoint)
        self.canvas.refresh()

    elif event.button() == QtCore.Qt.LeftButton:
        print("release clicked left")
        # The left click triggers the pan action
        iface.actionPan().trigger()

def showPoint(self, point):
    """ Adds point in qgis main window """

    # Using a vertex marker to show the selected point as a red cross
    m = QgsVertexMarker(self.canvas)
    m.setCenter(point)
    m.setColor(QColor(255,0,0))
    m.setIconSize(10)
    m.setIconType(QgsVertexMarker.ICON_X)

    self.canvas.refresh()

How can I keep my right-click behaviour AND still have normal left-click behaviour?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.