Qt滑鼠事件 我的滑鼠樣式
阿新 • • 發佈:2019-02-15
滑鼠樣式的效果如下:
主要思路:
重寫widget的MouseMoveEvent事件的處理函式。移動過程中的四個星星由四個定時器控制著,分別處於移動前位置A點和移動後的位置B點之間的四點上,然後從最小的星星到最大的星星分別在定時器事件中消失。
關於起點A和重點B的位置儲存問題,在mouseMoveEvent函式中,有pos()獲取到的座標是移動此刻的座標。
為了直白地表述這個過程,程式碼直接在widget的建構函式中完成,較少用到函式。程式碼如下:
widget.h
widget.cpp#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
#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錄製軟體錄下的結果