节前分享一个鼠标样式动画,祝大家春节快乐!

念茜 念茜 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
要放假了,闲来无事,想到在浏览网页时,经常看到漂亮的鼠标动画,琢磨着在Qt里也美化一下,写个demo分享给大家。
先上效果图


细节可参阅我的blog :http://blog.csdn.net/yiyaaixuexi/article/details/7209982

不废话,上demo代码:


#ifndef WIDGET_H
#define WIDGET_H

#include <QtGui/QWidget>
#include <QTimer>
#include <QList>
#include <QPoint>

class Widget : public QWidget
{
Q_OBJECT

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

private:
QWidget *w1,*w2,*w3,*w4;
QTimer *timer;
QList<QPoint> pos_list_w2;
QList<QPoint> pos_list_w3;
QList<QPoint> pos_list_w4;
int count;
void setMyStyle(QWidget *wid, int w, int h);

protected:
void mouseMoveEvent(QMouseEvent *event);

private slots:
void updatePos();
};

#endif // WIDGET_H




#include "widget.h"
#include <QPalette>
#include <QDebug>
#include <QMouseEvent>

Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QPalette phome = this->palette();
phome.setBrush(this->backgroundRole(), QBrush(QPixmap(":/images/bg.jpg")));
this->setPalette(phome);

w1 = new QWidget(this);
w2 = new QWidget(this);
w3 = new QWidget(this);
w4 = new QWidget(this);

setMyStyle(w1,34,34);
setMyStyle(w2,26,26);
setMyStyle(w3,18,18);
setMyStyle(w4,9,9);

timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(updatePos()));
timer->start(100);

this->setMouseTracking(true);

count = 0;
}

Widget::~Widget()
{

}

void Widget::setMyStyle(QWidget *wid, int w, int h)
{
wid->setWindowFlags(Qt::FramelessWindowHint);
wid->resize(w,h);
wid->setStyleSheet("border-image: url(:/images/cursor.png);");
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{

w1->move(event->x(),event->y());
if(!count)
pos_list_w2 << event->pos();
count > 7 ? (count = 0): count++;

}

void Widget::updatePos()
{
if(!pos_list_w4.isEmpty())
{
this->w4->move(pos_list_w4.takeFirst());
}
if(!pos_list_w3.isEmpty())
{
pos_list_w4 << pos_list_w3.first();
this->w3->move(pos_list_w3.takeFirst());
}

if(!pos_list_w2.isEmpty())
{
pos_list_w3 << pos_list_w2.first();
this->w2->move(pos_list_w2.takeFirst());
}
}


...全文

相关推荐

评论 189
收藏 已收藏
分享
返回顶部
念茜
Qt 21,503 成员 26,693 内容