用vs2015和qt5实现飞舞的蝴蝶
新建Qt Widgets Application,项目名为"Butterfly",基类选择"QMainWindow"。
Butterfly.h
#pragma once#include <QtWidgets/QMainWindow>
#include "ui_Butterfly.h"
#include<QObject>
#include<QtWidgets/QGraphicsItem>
#include<QPainter>
#include<QtWidgets/QGraphicsScene>
#include<QtWidgets/QGraphicsView>
class Butterfly : public QObject,public QGraphicsItem
{Q_OBJECTpublic:explicit Butterfly(QObject *parent = Q_NULLPTR);void timerEvent(QTimerEvent *);QRectF boundingRect() const;
signals:
public slots:
protected:void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); //重绘函数
private:bool up;QPixmap pix_up; //用于表示两幅蝴蝶的图片QPixmap pix_down;qreal angle;Ui::ButterflyClass ui; //这个没啥用,可写可不写
};
Butterfly.cpp
#include "Butterfly.h"
#include <math.h>
const static double PI = 3.1416;
Butterfly::Butterfly(QObject *parent): QObject(parent)
{up = true; //给标志蝴蝶翅膀位置的变量赋初值pix_up.load("D:/coding/qtPractice/Butterfly/Butterfly/x64/Debug/up.jpg"); //调用QPixmap的load()函数加载所用到的图片pix_down.load("D:/coding/qtPractice/Butterfly/Butterfly/x64/Debug/down.jpg");startTimer(100); //启动定时器,并设置时间间隔为100毫秒// ui.setupUi(this);
}
QRectF Butterfly::boundingRect() const
{qreal adjust = 2;return QRectF(-pix_up.width() / 2 - adjust, -pix_up.height() / 2 - adjust, pix_up.width() + adjust * 2, pix_up.height() + adjust * 2);
}void Butterfly::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;}
}void Butterfly::timerEvent(QTimerEvent *)
{//边界控制qreal edgex = scene()->sceneRect().right() + boundingRect().width() / 2; //限定蝴蝶飞舞的有边界qreal edgetop = scene()->sceneRect().top() + boundingRect().height() / 2; //限定蝴蝶飞舞的上边界qreal edgebottom = scene()->sceneRect().bottom() + boundingRect().height() / 2; //限定蝴蝶飞舞的下边界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());angle += (qrand() % 10) / 20.0;qreal dx = fabs(sin(angle*PI)*10.0);qreal dy = (qrand() % 20 - 10.0);setPos(mapToParent(dx, dy));
}
main.cpp
#include "Butterfly.h"
#include <math.h>
const static double PI = 3.1416;
Butterfly::Butterfly(QObject *parent): QObject(parent)
{up = true; //给标志蝴蝶翅膀位置的变量赋初值pix_up.load("D:/coding/qtPractice/Butterfly/Butterfly/x64/Debug/up.jpg"); //调用QPixmap的load()函数加载所用到的图片pix_down.load("D:/coding/qtPractice/Butterfly/Butterfly/x64/Debug/down.jpg");startTimer(100); //启动定时器,并设置时间间隔为100毫秒// ui.setupUi(this);
}
QRectF Butterfly::boundingRect() const
{qreal adjust = 2;return QRectF(-pix_up.width() / 2 - adjust, -pix_up.height() / 2 - adjust, pix_up.width() + adjust * 2, pix_up.height() + adjust * 2);
}void Butterfly::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;}
}void Butterfly::timerEvent(QTimerEvent *)
{//边界控制qreal edgex = scene()->sceneRect().right() + boundingRect().width() / 2; //限定蝴蝶飞舞的有边界qreal edgetop = scene()->sceneRect().top() + boundingRect().height() / 2; //限定蝴蝶飞舞的上边界qreal edgebottom = scene()->sceneRect().bottom() + boundingRect().height() / 2; //限定蝴蝶飞舞的下边界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());angle += (qrand() % 10) / 20.0;qreal dx = fabs(sin(angle*PI)*10.0);qreal dy = (qrand() % 20 - 10.0);setPos(mapToParent(dx, dy));
}
这里注意的是,我存取图片的命令是使用图片的绝对地址。图片如下:
用vs2015和qt5实现飞舞的蝴蝶相关推荐
- 【Qt5开发及实例】20、实现一个飞舞的蝴蝶
实现一个飞舞的蝴蝶 1.大体框架 首先创建一个场景,然后把图片元加入到这个场景中去,最后我们再创建一个显示层,把设置一下场景和图片元,拿来显示 这个是主函数main.cpp /** * 书本:[Qt5 ...
- 飞舞的蝴蝶(GraphicsView框架)
飞舞的蝴蝶(GraphicsView框架) 一.简介 GraphicsView框架结构主要包含三个主要的类QGraphicsScene(容器).QGraphicsView(视图).QGraphicsI ...
- Qt 动画飞舞的蝴蝶源码
一.简介 GraphicsView框架结构主要包含三个主要的类QGraphicsScene(容器).QGraphicsView(视图).QGraphicsItem(图形项).QGraphicsScen ...
- QT QGraphicsItem飞舞的蝴蝶
一效果 二源代码 1butterfly.h #ifndef BUTTERFLY_H #define BUTTERFLY_H#include<QObject> #include<QGr ...
- VS2015编译Qt5.7.0生成支持XP的静态库
一.编译工具 1.VS2015 编译Qt5.7.0的所需VS版本:Visual Studio 2013 (Update1)或Visual Studio 2015 (Update2).因为Update补 ...
- Qt浅谈之十七:飞舞的蝴蝶(GraphicsView框架)
一.简介 GraphicsView框架结构主要包含三个主要的类QGraphicsScene(容器).QGraphicsView(视图).QGraphicsItem(图形项).QGraphicsScen ...
- Qt5开发及实例学习之飞舞的蝴蝶
2019独角兽企业重金招聘Python工程师标准>>> 1.新建Qt GUI应用,基类QMainWindows,取消创建界面 2.项目名称上选择"添加新文件"-- ...
- VS2015配置QT5.X环境
最近VS2015支持qt-vs-addin了 最近在配置VS2015+QT5环境,网上教程几乎千篇一律,总结起来几乎都是:安装VS2015,安装qt5,最麻烦还要手动设置环境变量,最后好像还有安装个什 ...
- 如何使用VS2015开发Qt5程序
Qt是一个开发C++图形用户界面十分流行的开源库,Visual Studio是一个十分强大的集成开发环境,这两个强强联合起来进行C++应用程序的开发将是十分地方便快捷.下面就给大家介绍一下如何在VS2 ...
最新文章
- HTML5手机页面里面如何把长按复制避免
- 北斗导航 | 学习PPP和PPP-RTK
- 计算机组成微程序设计,微程序设计
- D - Undoubtedly Lucky Numbers CodeForces - 244B(数论 )
- Android之靠谱的获取本地相册图片
- 外存中的对换区和文件区
- H5红包互换源码 免公众号+对接支付完美营运 附视频教程
- 机器学习笔记(一)----基本概念
- 将你一张表的值覆盖_粉一张移动小蓝卡,智慧出行潮选择
- Win10电脑如何批量修改文件名
- 【模拟】NCPC 2014 E ceremony
- pytorch 学习1
- 编写java项目如何分层_我项目中的代码都是如何分层的?
- redis和couchbase的比较
- thrift开源项目研究
- 前端答题小游戏_前端知识小游戏
- 怎么把PDF转换成图片?教大家两种方式转换
- sdn 华硕二级路由_Mesh分布式路由器,能否改善全屋WiFi信号覆盖问题,路由组网分享...
- 安全基线规范之Cisco核心交换机
- 泛函分析--from BBS 水木清华站
热门文章
- HDU 2188-悼念512汶川大地震遇难同胞——选拔志愿者
- ElasticSearch学习2_Java version:1.7.0_51 suffers from critical bug及ES对JDK版本要求
- PAT-1032 挖掘机技术哪家强
- adb指令禁用软件_Android免root禁用系统应用(adb停用安卓系统应用)
- 三行代码按键消抖 独立按键 矩阵按键 长按 短按 双击
- 全国等级保护测评机构推荐目录
- initial-scale
- 求斐波那契数列的第n项
- 舌尖上的家乡——广东云浮/罗定
- android最新主流机型,就要Android 市售主流机型随你挑