QGraphicsItem实现动态蝴蝶(QT5开发及实例)

实现效果图:
在这里插入图片描述
实现代码:
butterfly.h

#ifndef BUTTERFLY_H
#define BUTTERFLY_H

#include <QObject>
#include<QGraphicsScene>
#include<QGraphicsView>
#include<QGraphicsItem>
#include<QPainter>
#include<QTimer>
class butterFly : public QObject , public QGraphicsItem
{
    Q_OBJECT
public:
    explicit butterFly(QObject *parent = nullptr);
    QRectF boundingRect() const;//为土元限定区域范围,所有继承QGraphicsItem的自定义图元都要实现此函数
signals:

public slots:
protected:
    void paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget);//重绘函数
private:
    QTimer *timer;//使用定时器切换 图片完成动画
    bool isUp;//标志蝴蝶状态
    QPixmap butter_up;
    QPixmap butterr_down;
    qreal angle;
};

#endif // BUTTERFLY_H

butterfly.cpp

#include "butterfly.h"
#include<math.h>
const static double PI=3.14;
butterFly::butterFly(QObject *parent)
{
    isUp=true;//初始化蝴蝶状态
    butter_up.load("://up.png");
    butterr_down.load(":/down.png");
    timer=new QTimer;
    timer->start(100);
    connect(timer,&QTimer::timeout,
            [=]()
    {
        qreal edgX=scene()->sceneRect().right()+boundingRect().width()/2;//限定蝴蝶的右边界
        qreal edgTop=scene()->sceneRect().top()+boundingRect().height()/2;//限定上边界
        qreal edgButtom=scene()->sceneRect().bottom()+boundingRect().height()/2;//限定下边界
        if(this->pos().x()>=edgX)//超过了右边界向左移动
        {
            this->setPos(scene()->sceneRect().left(),this->pos().y());
        }
        if(this->pos().y()>=edgTop)//超过了上边界向下移动
        {
            this->setPos(this->pos().x(),scene()->sceneRect().bottom());
        }
        if(this->pos().y()>=edgButtom)//超过了下边界向行移动
        {
            this->setPos(this->pos().x(),scene()->sceneRect().top());
        }
        angle+=(qrand()%10)/20;
        //相对于蝴蝶坐标的飞行路径
        qreal x=fabs(sin(angle*PI)*10);
        qreal y=(qrand()%20)-10;
        this->setPos(mapToParent(x,y));//映射为场景坐标
    });
}
QRectF butterFly::boundingRect() const
{
    qreal adjust =2;
    return QRectF(-butter_up.width()/2-adjust,-butter_up.height()/2-adjust,butter_up.width()+2*adjust,butter_up.height()+2*adjust);
}
void butterFly::paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget)
{
    if(isUp)
    {
        painter->drawPixmap(boundingRect().topLeft(),butter_up);
        isUp=!isUp;
    }
    else
    {
        painter->drawPixmap(boundingRect().topLeft(),butterr_down);
        isUp=!isUp;
    }
}

main.c

#include "mainwindow.h"
#include <QApplication>
#include<QGraphicsScene>
#include"butterfly.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsScene *scene=new QGraphicsScene;
    scene->setSceneRect(QRectF(-100,-100,-200,-200));
    butterFly *butter=new butterFly;
    butter->setPos(50,0);
    scene->addItem(butter);
    QGraphicsView *view=new QGraphicsView;
    view->setScene(scene);
    view->resize(400,400);
    view->show();
    return a.exec();
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值