• 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. python ansible api

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @File : test2.py # @Author: Anthony.waa # @Date : 20 ...

  2. win10下mysql安装过程中遇到的各种坑

    前几天重装系统,又要下回来mysql,但没想到还是遇到了许多麻烦,翻了十多篇博文才搞定,写个总结出来方便以后不要重复踩坑,也给大家参考参考. 1.下载与安装 这个没什么好说的,下载地址网上一大堆,安装 ...

  3. (转)Java进阶java int与Integer的区别

    Java进阶java int与Integer的区别 前言 int与Integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而Integer是对象,用一个引 ...

  4. arttemplate.js原生写法案例

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  5. Android 自定义控件之圆形扩散View(DiffuseView)

    实现效果 使用 属性方法 代码 源码下载 参考链接 实现效果 使用 XML中: <com.airsaid.diffuseview.widget.DiffuseView android:id=&q ...

  6. 【Oracle】管理还原数据(undo)

    1. 查看undo相关参数 SYS@LGR> show parameter undo NAME TYPE VALUE ------------------------------------ - ...

  7. 【Python3】POP3协议收邮件

    初学Python3,做一个email的例子,虽然知道做的很渣渣,还是分享一下吧 POP3协议 POP3全称Post Official Protocol3,即邮局协议的第三个版本,它规定了怎样将个人计算 ...

  8. Jquery数字转盘:

    项目中,在充值流程中,加入了1个抽奖环节,需要转盘显示抽中的虚拟货币.网上找了相关的特效,最后锁定在这个特效上:http://www.jb51.net/jiaoben/319636.html.因为用的 ...

  9. 2018秋招blibli算法工程师

    我给出代码如下:和之前做数塔(dp的入门题目)的思路一致 dp[i][j]为走到坐标(i,j)的最小减速(只有向右走和向上走两种情况) #include<stdio.h> #include ...

  10. python tips:类与实例的属性问题

    实例是具象化的类,它可以作为类访问所有静态绑定到类上的属性,包括类变量与方法,也可以作为实例访问动态绑定到实例上的属性. 实例1: class A: work = list("hello&q ...