Qt之检测鼠标移动

需求

在一个QWidget中,若是鼠标悬停3s以上,则需要弹出一个ToolTips来显示信息。

思路描述

重写 void mouseMoveEvent(QMouseEvent *event)
函数,然后创建一个定时器,用来计算鼠标是否保持不动3s。创建一个int类型变量 _timeCount,增长范围为0-2。当触发mouseMoveEvent 函数的时候 _timeCount 变量就自减,定时器则是每秒触发一次,让_timeCount自增。只要_timeCount达到3的时候,就显示ToolTips.

关键

void mouseMoveEvent(QMouseEvent *event)

此函数默认是在鼠标必须按下一个键,然后再移动鼠标的时候,才能触发此事件。
查阅帮助文档有:

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.

翻译过来就是:
如果禁用鼠标跟踪(默认),widget 只在鼠标移动时至少按下一个鼠标按钮时才能接收鼠标移动事件。
如果启用了鼠标跟踪,即使没有按下按钮,小部件也会接收鼠标移动事件。
因此需要调用 setMouseTracking(true); 来接收鼠标移动事件

代码

#pragma once

#include 
#include 
#include 
#include 
#include 
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);
}


你可能感兴趣的:(Qt,C/C++)