当前位置:网站首页>QT mouse tracking
QT mouse tracking
2022-06-25 13:09:00 【Bellerian】
One 、 Preface
Recently, a small project needs animation interaction : Mouse entry area , Show animation ( loop ); Mouse off area , Stop Animation ;
Ideas : It is certainly unrealistic to use video to play animation , So instead of QLabel Switch pictures to show animation ( There are enough video frames ). Inherit QLabel Override the class that displays animation , Define a region variable internally 、 A new state variable 、 An old state variable ;
If you enter the area , Then the old variable is equal to the new variable , The new variable is true; If you leave the area , Then the old variable is equal to the new variable , The new variable is false; Finally, if the two state quantities are different , It means that it needs to be changed :
- If the old variable is false, The new variable is true, It means that you have just entered the area , Play animation ;
- If the old variable is true, The new variable is false, It means that you have just left the area , Stop Animation ;
!!! Particular attention !!!
Mouse movement events mouseMoveEvent
By default, you need to click to move before triggering the event , Set up setMouseTracking(true)
After that, you can directly move to trigger the event , And can not be QMainWindow class , Use QWidget And its subclasses , This setting only works !
Two 、 Effect display
3、 ... and 、 Detailed code
#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); // You have to set this mouseMoveEvent You don't have to click the mouse to trigger
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(" Mouse into animation area ");
dialog->show();
}
}
}
边栏推荐
猜你喜欢
爱可可AI前沿推介(6.25)
中国虚拟人哪家强?沙利文、IDC:小冰百度商汤位列第一梯队
药物设计新福音:腾讯联合中科大、浙大开发自适应图学习方法,预测分子相互作用及分子性质
20220620 interview reply
Capabilities required by architects
Maui的学习之路(二)--设置
MySQL adds, modifies, and deletes table fields, field data types, and lengths (with various actual case statements)
J2EE from entry to earth 01 MySQL installation
Sword finger offer day 2 linked list (simple)
.NET in China - What's New in .NET
随机推荐
【AI助力科研】loss曲线傻瓜式绘制
torch.tensor拼接与list(tensors)
Oracle trigger error report table or view does not exist
[machine learning] parameter learning and gradient descent
Sword finger offer II 028 Flatten multi-level bidirectional linked list
重磅直播|BizDevOps:数字化转型浪潮下的技术破局之路
汇编标志位相关知识点(连)
时间过滤器(el-table)中使用
.NET in China - What's New in .NET
KDD 2022 | GraphMAE:自监督掩码图自编码器
康威定律,作为架构师还不会灵活运用?
词法陷阱(C)
字符串入门十八讲合集四
KVM 脚本管理 —— 筑梦之路
MySQL writes user-defined functions and stored procedure syntax (a detailed case is attached, and the problem has been solved: errors are reported when running user-defined functions, and errors are r
KDD 2022 | graphmae: self supervised mask map self encoder
剑指Offer 第 2 天链表(简单)
阿里稳定性之故障应急处理流程
Introduction to string 18 lectures Collection 4
3+1 guarantee: how is the stability of the highly available system refined?