Qt鼠标事件 我的鼠标样式

时间:2022-03-13 22:35:35

鼠标样式的效果如下:


Qt鼠标事件 我的鼠标样式


主要思路:


重写widget的MouseMoveEvent事件的处理函数。移动过程中的四个星星由四个定时器控制着,分别处于移动前位置A点和移动后的位置B点之间的四点上,然后从最小的星星到最大的星星分别在定时器事件中消失。


关于起点A和重点B的位置保存问题,在mouseMoveEvent函数中,有pos()获取到的坐标是移动此刻的坐标。


为了直白地表述这个过程,代码直接在widget的构造函数中完成,较少用到函数。代码如下:


widget.h


#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QEvent>
#include <QTimer>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();

protected:
void mouseMoveEvent(QMouseEvent *);

private:
Ui::Widget *ui;

//四个星星
QWidget *widget1;
QWidget *widget2;
QWidget *widget3;
QWidget *widget4;

//处理星星消失的四个定时器
QTimer *timer1;
QTimer *timer2;
QTimer *timer3;
QTimer *timer4;


int x, y; //鼠标起点坐标
int mouse_x, mouse_y; //移动后的坐标

private slots:
void time1(); //定时器事件槽
void time2();
void time3();
void time4();

};

#endif // WIDGET_H


widget.cpp


#include "widget.h"
#include "ui_widget.h"
#include <QPixmap>
#include <QLabel>


Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);

//分别为四个星星声明四个widget
widget1 = new QWidget(this);
widget1->setWindowFlags(Qt::FramelessWindowHint);
widget1->resize(20, 20);
widget1->setStyleSheet("border-image: url(:/Icons/love.png)");

widget2 = new QWidget(this);
widget2->setWindowFlags(Qt::FramelessWindowHint);
widget2->resize(20, 20);
widget2->setStyleSheet("border-image: url(:/Icons/love.png)");

widget3 = new QWidget(this);
widget3->setWindowFlags(Qt::FramelessWindowHint);
widget3->resize(20, 20);
widget3->setStyleSheet("border-image: url(:/Icons/love.png)");

widget4 = new QWidget(this);
widget4->setWindowFlags(Qt::FramelessWindowHint);
widget4->resize(20, 20);
widget4->setStyleSheet("border-image: url(:/Icons/love.png)");

//定时器
timer1 = new QTimer(this);
timer2 = new QTimer(this);
timer3 = new QTimer(this);
timer4 = new QTimer(this);

x = QCursor::pos().x(); //初始时鼠标位置
y = QCursor::pos().y();

connect(timer1, SIGNAL(timeout()), this, SLOT(time1()));
connect(timer2, SIGNAL(timeout()), this, SLOT(time2()));
connect(timer3, SIGNAL(timeout()), this, SLOT(time3()));
connect(timer4, SIGNAL(timeout()), this, SLOT(time4()));

setMouseTracking(true); //跟踪鼠标
}



void Widget::mouseMoveEvent(QMouseEvent *mouseMoveEvent)
{
mouse_x = QCursor::pos().x(); //获取移动后的鼠标位置
mouse_y = QCursor::pos().y();

widget4->setGeometry(mouse_x + 20, mouse_y - 10, 50, 50); //分别计算移动路径上的分布
widget3->setGeometry(mouse_x + 20 + (x - mouse_x)/(3.0), mouse_y - 10 + (y - mouse_y)/(3.0), 40, 40);
widget2->setGeometry(mouse_x + 20 + 2 * (x - mouse_x)/(3.0), mouse_y - 10 + 2 * (y - mouse_y)/(3.0), 30, 30);
widget1->setGeometry(mouse_x + 20 + 3 * (x - mouse_x)/(3.0), mouse_y - 10 + 3 * (y - mouse_y)/(3.0), 20, 20);


timer1->start(50); //开始定时器,来处理星星消失
timer2->start(100);
timer3->start(150);
timer4->start(200);

widget4->show();
widget3->show();
widget2->show();
widget1->show();
}

void Widget::time1()
{
widget1->hide();
timer1->stop();
}

void Widget::time2()
{
widget2->hide();
timer2->stop();
}

void Widget::time3()
{
widget3->hide();
timer3->stop();
}

void Widget::time4()
{
widget4->hide();
timer4->stop();
x = mouse_x;
y = mouse_y;
}

Widget::~Widget()
{
delete ui;
}

PS.用灵者GIF录制软件录下的结果