MouseMoveEvent in Qt is used in MainWindow

 

Recently, using the Qt software interface, you need to use mouseMoveEvent, researched, found some problems, and shared.

 

To capture mouse movement events in Qt, you need to rewrite MouseMoveEvent, but MouseMoveEvent is not captured by the mouse in the default state in order to not consume resources. To capture the movement of the mouse when it is not pressed, setMouseTracking(true) is required.

 

bool mouseTracking

 

This property holds whether the widget tracking mouse is in effect.

If mouse tracking is disabled (default), this widget will receive mouse movement events only when at least one mouse button is pressed when the mouse is moved.

If mouse tracking is in effect, this widget will receive mouse movement events even if no buttons are pressed.

 

There is no problem when using QWidget. However, even if setMouseTracking(true) is used for QMainWindow, the movement of the mouse cannot be captured when the mouse is not pressed. It can only be captured when the mouse is pressed.

 

Solution:

First use the CentrolWIdget of QMainWindow to use setMouseTracking(true) to enable mobile monitoring. Then turn on the monitoring in setMouseTracking(true) of QMainWindow. Everything was normal after that.

Cause Analysis:

CentrolWIdget is a subclass of QMainWindow. If you respond to mouse events on the subclass, only the mouseMoveEvent of the subclass will be triggered. According to the principle of C++ inheritance and overloading, the subclass must also setMouseTracking(true); so if you want to respond to the mouse The control of the event is contained by a parent control, then the control and its parent control or container also need setMouseTracking(true);

 

Guess you like

Origin blog.csdn.net/lt4959/article/details/12496677