当前位置:网站首页>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

 Please add a picture description


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();
        }
    }
}

原网站

版权声明
本文为[Bellerian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206251223251738.html