当前位置:网站首页>Qt鼠标跟踪
Qt鼠标跟踪
2022-06-25 12:23:00 【贝勒里恩】
一、前言
最近一个小项目需要动画交互:鼠标进入区域,显示动画(循环);鼠标离开区域,停止动画;
思路:动画采用视频播放肯定不现实,所以改为QLabel切换图片来显示动画(有足够的视频帧)。继承QLabel重写显示动画的类,在内部定义一个区域变量、一个新状态变量、一个旧状态变量;
如果进入区域,则旧变量等于新变量,新变量为true;如果离开区域,则旧变量等于新变量,新变量为false;最后如果两个状态量不同,则说明需要改变:
- 如果旧变量为false,新变量为true,则说明刚刚进入区域,播放动画;
- 如果旧变量为true,新变量为false,则说明刚刚离开区域,停止动画;
!!!特别注意!!!
鼠标移动事件 mouseMoveEvent 默认需要点击之后移动才触发事件,设置setMouseTracking(true)之后才可以直接移动就触发事件,且不能是QMainWindow类,要用QWidget及其子类,此设置才管用!
二、效果展示

三、详细代码
#ifndef WIDGET_H
#define WIDGET_H
#include <QLabel>
#include <QDebug>
#include <QMouseEvent>
#include <QDialog>
#include <QHBoxLayout>
class Widget : public QLabel
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
QRect annimation_rect;
bool old_flag;
bool new_flag;
QDialog* dialog;
QLabel* label;
protected:
void mouseMoveEvent(QMouseEvent *event);
signals:
void sig_flag(bool);
};
#endif // WIDGET_H
#include "widget.h"
Widget::Widget(QWidget* parent) : QLabel(parent)
{
this->setMouseTracking(true); //必须设置这个mouseMoveEvent才不用点击鼠标之后才触发
dialog = new QDialog(this);
dialog->resize(200,300);
dialog->setStyleSheet("background-color: red");
dialog->setWindowFlag(Qt::WindowStaysOnTopHint);
dialog->setWindowFlag(Qt::FramelessWindowHint);
QHBoxLayout* layout_dialog = new QHBoxLayout(dialog);
layout_dialog->setMargin(0);
layout_dialog->setSpacing(0);
label = new QLabel(dialog);
layout_dialog->addWidget(label);
}
Widget::~Widget()
{
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
QPoint mousePos = event->pos();
if(annimation_rect.contains(mousePos)) {
old_flag = new_flag;
new_flag = true;
}else {
old_flag = new_flag;
new_flag = false;
}
if(old_flag != new_flag) {
if(old_flag && !new_flag) {
sig_flag(false);
dialog->hide();
}
if(!old_flag && new_flag) {
sig_flag(true);
label->setText("鼠标进入动画区域");
dialog->show();
}
}
}
边栏推荐
- JS uses the for loop in the function to insert and delete the array at the specified position
- J2EE从入门到入土01.MySQL安装
- 2021-10-21
- Command line garbled
- Reload cuda/cudnn/pytorch
- The push(), pop(), unshift(), shift() method in the JS array
- Match regular with fixed format beginning and fixed end
- CUDA error: unspecified launch failure
- MySQL adds, modifies, and deletes table fields, field data types, and lengths (with various actual case statements)
- 剑指 Offer II 025. 链表中的两数相加
猜你喜欢

Koa frame

Elemntui's select+tree implements the search function

Geospatial search - > R tree index

@Scheduled implementation of scheduled tasks (concurrent execution of multiple scheduled tasks)

GPS receiver design (1)

地理空间搜索 ->R树索引

The drop-down box renders numbers instead of the corresponding text. How to deal with it

剑指 Offer II 025. 链表中的两数相加

C program linking SQLSERVER database: instance failed

二叉树之_哈夫曼树_哈弗曼编码
随机推荐
量化交易之回测篇 - 期货CTA策略策略(TQZFutureRenkoWaveStrategy)
线上服务应急攻关方法论
Talk about 11 key techniques of high availability
Go novice exploration pause
Write regular isosceles triangle and inverse isosceles triangle with for loop in JS
1251- Client does not support authentication protocol MySql错误解决方案
2021-10-21
Resolved: could not find artifact XXX
Three jobs! You can learn this from me (attached with graduation vlog)
浏览器的5种观察器
flutter 收到推送后自动消失问题
剑指offer 第 3 天字符串(简单)
MySQL adds, modifies, and deletes table fields, field data types, and lengths (with various actual case statements)
剑指 Offer 04. 二维数组中的查找
Visual studio2019 link opencv
515. Find Largest Value in Each Tree Row
PPT绘图之AI助力论文图
模块五(微博评论)
Koa frame
visual studio2019链接opencv