• fly.h

     #ifndef FLY_H
    #define FLY_H
    #include <QObject>
    #include <QPainter>
    #include <QGraphicsScene>
    #include <QGraphicsView>
    #include <QGraphicsItem>
    #include <QPixmap>
    #include <math.h>
    #include <QTime>
    #include <QEvent> //图元对象
    class fly : public QObject,public QGraphicsItem
    {
    Q_OBJECT
    public:
    explicit fly(QObject *parent = nullptr); //区域
    QRectF boundingRect() const;
    //时间定时器
    void timerEvent(QTimerEvent *event);
    signals: public slots: protected:
    //绘制
    void paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget); public:
    bool up;
    QPixmap pix_up;
    QPixmap pix_down; qreal angle; }; #endif // FLY_H
  • fly.cpp
     #include "fly.h"
    
     fly::fly(QObject *parent) : QObject(parent)
    {
    //载入
    up = true;
    pix_up.load("up.png");
    pix_down.load("down.png"); startTimer();
    } //图元的边界区域
    QRectF fly::boundingRect() const
    {
    qreal angletemp = ;
    return QRectF(-pix_up.width()/-angletemp,
    -pix_up.height()/-angletemp,
    pix_up.width()+angletemp*,
    pix_up.height()+angletemp*);
    } //定时器函数
    void fly::timerEvent(QTimerEvent *event)
    {
    //边界控制
    qreal edgex = scene()->sceneRect().right()+boundingRect().width()/;
    qreal edgetop=scene()->sceneRect().top()+boundingRect().height()/;
    qreal edgebottom = scene()->sceneRect().bottom()+boundingRect().height()/; if(pos().x()>=edgex)
    setPos(scene()->sceneRect().left(),pos().y());
    if(pos().y()<=edgetop)
    setPos(pos().x(),scene()->sceneRect().bottom());
    if(pos().y()>=edgebottom)
    setPos(pos().x(),scene()->sceneRect().top()); #define PI 3.14
    angle += (qrand() % )/20.0;
    qreal dx = fabs(sin(angle*PI)*);
    qreal dy = (qrand()%)-10.0; //设置位置,转换坐标
    setPos(mapToParent(dx,dy));
    } void fly::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
    if(up)
    {
    painter->drawPixmap(boundingRect().topLeft(),pix_up);
    up = !up;
    }
    else
    {
    painter->drawPixmap(boundingRect().topLeft(),pix_down);
    up = !up;
    }
    }
  • main.cpp
     #include "dialog.h"
    #include <QApplication>
    #include <QGraphicsScene>
    #include <QGraphicsView>
    #include "fly.h" int main(int argc, char *argv[])
    {
    QApplication a(argc, argv); //创建图像场景
    QGraphicsScene *scene = new QGraphicsScene;
    //设置区域
    scene->setSceneRect(-,-,,); fly *f = new fly;
    f->setPos(-,);
    scene->addItem(f); QGraphicsView *view = new QGraphicsView;
    view->setScene(scene);
    view->resize(,);
    view->show(); return a.exec();
    }

35.QT蝴蝶飞舞的更多相关文章

  1. Qt-MVC图形视图框架初识

    使用QPushButton.QLabel.QCheckBox等构成GUI的控件或自定义图形时,开发应用程序会变得很简单.但是如果想在GUI中使用数十个或者数百个图形对象,向用户完美展示控制场景,则会受 ...

  2. Linux - 变量

    printenv - print all or part of environment 显示所有变量:print 显示某个变量:print <variable name> 或者 echo ...

  3. Ubuntu终端命令行不显示颜色

    在网上找到的一个有效方案是在.bash_profile 中增加颜色定义 export LS_COLORS='di=01;35:ln=01;36:pi=40;33:so=01;35:do=01;35:b ...

  4. ubuntu 14.04 下找不到命令,路径出错

    在安装一些东西时,可能操作上不小心把路径覆盖或打错,造成一些基本命令如ls mkdir等无法使用,就会出现以下提示错误. 错误: 由于/usr/bin 不在PATH 环境变量中,故无法找到该命令 根本 ...

  5. crontab不能正确执行的问题

    近期在部署crontab任务的时候,总是遇到在shell中单独执行正常,但是放到crontab定时执行出错的问题.若出现这类场景,九成就是环境变量的问题. 因为我的定制任务,基本上都需要使用sqlpl ...

  6. python模块(os)

    os模块 os模块提供了许多与操作系统交互的接口 os.getcwd() -> str # 返回当前路径, 相当于pwd os.chdir("dirname") -> ...

  7. 查看linux系统常用的命令,Linux查看系统配置常用命令

    一.linux CPU大小  cat /proc/cpuinfo |grep "model name" && cat /proc/cpuinfo |grep &qu ...

  8. ansible官方文档翻译之变量

    Ansible变量 在使用ansible变量的时候,主要是因为各个系统的不同,从而需要使用不同的变量来进行设置,例如在设置一些配置文件的时候,有大部分内容是相同的,但是一部分内容是和主机的ip地址或者 ...

  9. 6款基于SVG的HTML5应用和动画

    1.HTML5 SVG 3D蝴蝶飞舞动画 逼真超酷 这次我们要分享的这款HTML5动画简直就是逆天,利用SVG制作的3D蝴蝶飞舞动画,蝴蝶飞舞动画非常逼真,蝴蝶飞舞的路线是利用SVG构造的.另外,动画 ...

随机推荐

  1. elasticsear+kibana+logstash 优化

    ##关于s2安装cerebro来可视化集群管理##### https://blog.csdn.net/RWSLR6/article/details/79648767 https://github.co ...

  2. JavaScriptResult

  3. 电商物流仓储WMS业务流程

    电商物流仓储WMS业务流程 SKU是什么意思?  一文详解电商仓储管理中SKU的含义 从货品角度看,SKU是指单独一种商品,其货品属性已经被确定.只要货品属性有所不同,那么就是不同的SKU. PO信息 ...

  4. AndroidStudio/Intellij 快捷键

    说明 三年来一直使用Eclipse作为自己的IDE, 现在是时候走出自己的safety zone, 开始使用传说中的Intellij了. Eclipse/Intellij IDE环境为: OS X 1 ...

  5. 04--深入探讨C++中的引用

    深入探讨C++中的引用           引用是C++引入的新语言特性,是C++常用的一个重要内容之一,正确.灵活地使用引用,可以使程序简洁.高效.我在工作中发现,许多人使用它仅仅是想当然,在某些微 ...

  6. Difference between == and ===

    In swift 3 and above === (or !==) Checks if the values are identical (both point to the same memory ...

  7. 软件的描述x

    1)文档描述: 2)模型描述: 3)架构描述: 4)代码描述:

  8. WIN系统查询版本

    cmd -> DISM /online /Get-CurrentEdition //查询系统版本 WIN+R -> slmgr.vbs -ipk 查询系统注册信息slmgr.vbs -dl ...

  9. MVC 先后顺序

    @foreach (var item in Model) { if (ViewBag.GetModel.ParentID == item.DictID) { <option value=&quo ...

  10. Java中数组的定义方式

    数组定义方式一 动态方式(指定数组的长度) 格式: 数组存储的数据类型[]数组名字 = new 数组存储的数据类型[长度]; [] : 表示数组. 数组名字:为定义的数组起个变量名,满足标识符规范,可 ...