Qt鼠标事件 我的鼠标样式
2015-08-21 20:49
656 查看
鼠标样式的效果如下:
![](http://img.blog.csdn.net/20150821204843102)
主要思路:
重写widget的MouseMoveEvent事件的处理函数。移动过程中的四个星星由四个定时器控制着,分别处于移动前位置A点和移动后的位置B点之间的四点上,然后从最小的星星到最大的星星分别在定时器事件中消失。
关于起点A和重点B的位置保存问题,在mouseMoveEvent函数中,有pos()获取到的坐标是移动此刻的坐标。
为了直白地表述这个过程,代码直接在widget的构造函数中完成,较少用到函数。代码如下:
widget.h
widget.cpp
PS.用灵者GIF录制软件录下的结果
主要思路:
重写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录制软件录下的结果
相关文章推荐
- css3背景颜色渐变
- NumberPicker 样式设置
- CSS中行高line-height属性的一些使用技巧
- CSS学习笔记之定位
- css position
- css3 transform transition 实现照片墙效果
- CSS3:绘制图形
- CSS实现绝对底部一个完美解决方案
- HTML简介(css简单的样式)
- 网页样式(css+js)tab自动切换的minisite
- css3 display:box
- CSS 3层嵌套居中布局
- CSS float 父元素高度自适应
- css3选择器
- RunJS推荐用于个人使用(使用方便JS、css实时预览、编辑、管理等功能)
- Qt自己的css简称qss
- Firebug折腾记_(2)HTML&CSS定位及调试小技巧
- Firebug折腾记_(2)HTML&CSS定位及调试小技巧
- 纯CSS3制作的圆角效果按钮菜单
- CSS 简单样式