QgsMapMouseEvent class

A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.

It is sent whenever the user moves, clicks, releases or double clicks the mouse. In addition to the coordinates in pixel space it also knows the coordinates in the mapcanvas' CRS as well as it knows the concept of snapping.

Constructors, destructors, conversion operators

QgsMapMouseEvent(QgsMapCanvas* mapCanvas, QMouseEvent* event)
Creates a new QgsMapMouseEvent.
QgsMapMouseEvent(QgsMapCanvas* mapCanvas, QEvent::Type type, QPoint pos, Qt::MouseButton button = Qt::NoButton, Qt::MouseButtons buttons = Qt::NoButton, Qt::KeyboardModifiers modifiers = Qt::NoModifier)
Creates a new QgsMapMouseEvent.

Public functions

auto isSnapped() const -> bool
Returns true if there is a snapped point cached.
auto mapPoint() const -> QgsPointXY
mapPoint returns the point in coordinates
auto mapPointMatch() const -> QgsPointLocator::Match
Returns the matching data from the most recently snapped point.
auto originalMapPoint() const -> QgsPointXY
Returns the original, unmodified map point of the mouse cursor.
auto originalPixelPoint() const -> QPoint
The unsnapped, real mouse cursor position in pixel coordinates.
auto pixelPoint() const -> QPoint
The snapped mouse cursor in pixel coordinates.
void setMapPoint(const QgsPointXY& point)
Set the (snapped) point this event points to in map coordinates.
auto snapPoint() -> QgsPointXY
snapPoint will snap the points using the map canvas snapping utils configuration
void snapToGrid(double precision, const QgsCoordinateReferenceSystem& crs)
Snaps the mapPoint to a grid with the given precision.

Function documentation

QgsMapMouseEvent::QgsMapMouseEvent(QgsMapCanvas* mapCanvas, QMouseEvent* event)

Creates a new QgsMapMouseEvent.

Parameters
mapCanvas The map canvas on which the event occurred
event The original mouse event

Should only be required to be called from the QgsMapCanvas.

QgsMapMouseEvent::QgsMapMouseEvent(QgsMapCanvas* mapCanvas, QEvent::Type type, QPoint pos, Qt::MouseButton button = Qt::NoButton, Qt::MouseButtons buttons = Qt::NoButton, Qt::KeyboardModifiers modifiers = Qt::NoModifier)

Creates a new QgsMapMouseEvent.

Parameters
mapCanvas The map canvas on which the event occurred
type The type of the event
pos The pixel position of the mouse
button The pressed button
buttons Further buttons that are pressed
modifiers Keyboard modifiers

Should only be required to be called from the QgsMapCanvas.

bool QgsMapMouseEvent::isSnapped() const

Returns true if there is a snapped point cached.

Returns True if there is a snapped point cached.

Will only be useful after snapPoint has previously been called.

QgsPointXY QgsMapMouseEvent::mapPoint() const

mapPoint returns the point in coordinates

Returns the point in map coordinates, after snapping if requested in the event.

QgsPointLocator::Match QgsMapMouseEvent::mapPointMatch() const

Returns the matching data from the most recently snapped point.

Returns the snapping data structure

QgsPointXY QgsMapMouseEvent::originalMapPoint() const

Returns the original, unmodified map point of the mouse cursor.

Returns The cursor position in map coordinates.

QPoint QgsMapMouseEvent::originalPixelPoint() const

The unsnapped, real mouse cursor position in pixel coordinates.

Returns Mouse position in pixel coordinates

Alias to pos()

QPoint QgsMapMouseEvent::pixelPoint() const

The snapped mouse cursor in pixel coordinates.

Returns The snapped mouse cursor position in pixel coordinates.

void QgsMapMouseEvent::setMapPoint(const QgsPointXY& point)

Set the (snapped) point this event points to in map coordinates.

Parameters
point The point in map coordinates

The point in pixel coordinates will be calculated accordingly.

QgsPointXY QgsMapMouseEvent::snapPoint()

snapPoint will snap the points using the map canvas snapping utils configuration

void QgsMapMouseEvent::snapToGrid(double precision, const QgsCoordinateReferenceSystem& crs)

Snaps the mapPoint to a grid with the given precision.

The snapping will be done in the specified crs. If this crs is different from the mapCanvas crs, it will be reprojected on the fly.