Qt literacy - QMouseEvent mouse event

I. Overview

Mouse events occur when the mouse button is pressed or released , or the mouse cursor is moved , within the QWidget window .

There is nothing special about mouse press and release, but mouse move events are special. Mouse move events will only occur when the mouse button is pressed , unless mouse tracking is enabled using the QWidget::setMouseTracking() function.

  • QWidget::setMouseTracking(bool enable)
    If mouse tracking is disabled (default), the Widget will only receive mouse movement events if at least one mouse button is pressed during mouse movement.
    If mouse tracking is enabled, the widget will receive mouse movement events even when no button is pressed.

Second, the transmission of mouse events

When the mouse button inside the widget is pressed, Qt will automatically grab the mouse; the widget will continue to receive mouse events until the last mouse button is released.
Mouse events contain a special accept flag that indicates whether the receiver wants the event. If the widget does not handle mouse events, ignore() should be called. This operation allows the mouse event to propagate up the parent Widget chain until the Widget accepts it using accept(), or the event filter consumes it.

Note: If a mouse event is propagated to a Widget with Qt::wa_nomoussepropagation set, the mouse event will not be propagated further up the parent Widget chain.

3. Combination modifiers

The state of keyboard modifier keys can be found by calling the modifiers() function inherited from QInputEvent.

It's like I hold down some shift or alt while the mouse is moving. This can be when multiple keys are pressed at the same time,

identification name value meaning
Qt::NoModifier 0x00000000 no modifier keys pressed
Qt::ShiftModifier 0x02000000 Shift press
Qt::ControlModifier 0x04000000 Ctrl pressed
Qt::AltModifier 0x08000000 Alt press
Qt::MetaModifier 0x10000000 Meta press

4. Mouse coordinate position

The functions pos(), x() and y() give the position of the mouse relative to the Widget receiving the mouse event.

If the Widget is moved after a mouse event, use the global position returned by globalPos() to avoid jittering. This globalPos() is actually the position of the mouse on the entire screen plane, and pos() refers to the position of the mouse in this window.
insert image description here

5. How to use

The function QWidget::setEnabled() can be used to enable or disable the mouse and keyboard events of the Widget.

When using this event function, you need to reimplement the QWidget event handler, that is, inherit the QWidget window to rewrite QWidget::mousePressEvent(), QWidget::mouseReleaseEvent(), QWidget::mouseDoubleClickEvent() and QWidget::mouseMoveEvent( ), you can receive mouse events in your own Widget. Of course, you don't need to inherit, you can also use the event filter directly.

Guess you like

Origin blog.csdn.net/qq_43680827/article/details/131576395