Qt's detection of mouse movement

demand

In a QWidget, if the mouse is hovered for more than 3 seconds, a ToolTips needs to pop up to display information.

Idea description

Rewrite void mouseMoveEvent(QMouseEvent *event)
function, and then create a timer used to calculate whether the mouse to remain intact 3s. Create an int type variable _timeCount with a growth range of 0-2. When the mouseMoveEvent function is triggered, the _timeCount variable is decremented, and the timer is triggered once every second, and _timeCount is incremented. As long as _timeCount reaches 3, ToolTips will be displayed.

The essential

void mouseMoveEvent(QMouseEvent *event)

By default, this function triggers this event when the mouse must press a key and then move the mouse.
Refer to the help documents:

This property holds whether mouse tracking is enabled for the widget
If mouse tracking is disabled (the default), the widget only receives mouse move events when at least one mouse button is pressed while the mouse is being moved.
If mouse tracking is enabled, the widget receives mouse move events even if no buttons are pressed.

The translation is:
if mouse tracking is disabled (default), the widget can only receive mouse movement events when at least one mouse button is pressed while the mouse is moving.
If mouse tracking is enabled, the widget will receive mouse movement events even if the button is not pressed.
So we need to call setMouseTracking(true);to receive mouse motion events

Code

#pragma once

#include <QWidget>
#include <QLabel>
#include <QTimer>
#include <QMouseEvent>
#include <QSharedPointer>
class FToolTipsLabel : public QLabel
{
    
    
	Q_OBJECT
public:
	FToolTipsLabel(QWidget *parent = nullptr);
	~FToolTipsLabel();
	void setFillColor(const QColor &fillColor);
	void setToolTips(const QString &tips);
	void setShowOrHide(bool flag);
private:
	void initShowState();
	void setUpConnection();
	private slots:
	void onShowOrHide();
private:
	QColor _fillColor;
	QTimer* _timerShow;//用来控制ToolTips显示的时间
	bool _isShow;//是否显示
	int _curSecond;
};
class FMouseMoveTest : public QWidget
{
    
    
	Q_OBJECT

public:
	FMouseMoveTest(QWidget *parent = Q_NULLPTR);
	~FMouseMoveTest();
private:
	void setUpConnections();
	void updateTimeCount();
private slots:
	void onTimerOut();
protected:
	virtual void mouseMoveEvent(QMouseEvent *event) override;
	virtual void mousePressEvent(QMouseEvent *event)override;
	virtual void mouseReleaseEvent(QMouseEvent *event) override;
private:
	FToolTipsLabel* _labelToolTips;
	int _timeCount;
	QPoint _mousePoint;
	QTimer* _timeMouseMove;
};

#include "FMouseMoveTest.h"

/*ToolTipsLabel*/
FToolTipsLabel::FToolTipsLabel(QWidget *parent /*= nullptr*/)
	:QLabel(parent)
	, _fillColor(QColor(255, 255, 255))
	, _isShow(true)
	, _timerShow(nullptr)
	, _curSecond(0)
{
    
    
	setUpConnection();
	setStyleSheet("QLabel{color:#000000;background-color:#FFFFFF;border:1px solid #000000;}");
	hide();
}

FToolTipsLabel::~FToolTipsLabel()
{
    
    

}

void FToolTipsLabel::setFillColor(const QColor &fillColor)
{
    
    
	_fillColor = fillColor;
}

void FToolTipsLabel::setToolTips(const QString &tips)
{
    
    
	this->setText(tips);
	this->adjustSize();
	initShowState();
}

void FToolTipsLabel::setShowOrHide(bool flag)
{
    
    
	_isShow = flag;
}

void FToolTipsLabel::initShowState()
{
    
    
	_curSecond = 0;
	setShowOrHide(true);
}

void FToolTipsLabel::setUpConnection()
{
    
    
	_timerShow = new QTimer(this);
	connect(_timerShow, &QTimer::timeout, this, &FToolTipsLabel::onShowOrHide);
	_timerShow->start(1000);
}

void FToolTipsLabel::onShowOrHide()
{
    
    
	_curSecond++;
	if (_curSecond == 3)
	{
    
    
		_isShow = false;
		_curSecond = 0;
	}
	if (_isShow)
	{
    
    
		this->show();
	}
	else
	{
    
    
		this->hide();
	}
}

FMouseMoveTest::FMouseMoveTest(QWidget *parent)
	: QWidget(parent)
	, _timeCount(0)
{
    
    
	_labelToolTips = new FToolTipsLabel(this);
	_timeMouseMove = new QTimer;
	setUpConnections();
	_timeMouseMove->start(1000);
	setMouseTracking(true);//开启鼠标追踪
}

FMouseMoveTest::~FMouseMoveTest()
{
    
    
}

void FMouseMoveTest::setUpConnections()
{
    
    
	connect(_timeMouseMove, &QTimer::timeout, this, &FMouseMoveTest::onTimerOut);
}	

void FMouseMoveTest::updateTimeCount()
{
    
    
	_timeCount--;
	if (_timeCount < 0)
	{
    
    
		_timeCount = 0;
	}
}

void FMouseMoveTest::onTimerOut()
{
    
    
	_timeCount++;
	if (_timeCount == 3)
	{
    
    
		_timeCount = 0;
		_labelToolTips->setToolTips("Qt Tool Tips");
		_labelToolTips->move(_mousePoint);
	}
}

void FMouseMoveTest::mouseMoveEvent(QMouseEvent *event)
{
    
    
	_mousePoint = event->pos();
	updateTimeCount();
	return QWidget::mouseMoveEvent(event);
}

void FMouseMoveTest::mousePressEvent(QMouseEvent *event)
{
    
    
	_mousePoint = event->pos();
	updateTimeCount();
	return QWidget::mousePressEvent(event);
}

void FMouseMoveTest::mouseReleaseEvent(QMouseEvent *event)
{
    
    
	_mousePoint = event->pos();//存储鼠标最后悬停的地点
	updateTimeCount();
	return QWidget::mouseReleaseEvent(event);
}


Guess you like

Origin blog.csdn.net/weixin_39308337/article/details/105343158