参考:Qt4 开发实践第八章 图形视图QGraphicsView

#ifndef DRIVEDGRAPH_H
#define DRIVEDGRAPH_H #include <QObject>
#include <QGraphicsItem>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QPainter> class DrivedGraph : public QObject,public QGraphicsItem
{
Q_OBJECT
public:
DrivedGraph();
void timerEvent(QTimerEvent *); // 在定时器里对QGraphicsItem 进行重画
QRectF boundingRect() const; // 为图元限定区域范围,所有继承自QGraphicsItem的自定义图元都必须实现此函数
protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); // 重画函数,继承自QGraphicsItem
private:
bool m_bupFlag;
QPixmap m_pixUp;
QPixmap m_pixDown;
qreal m_angle;
}; #endif // DRIVEDGRAPH_H
#include "drivedgraph.h"
#include <math.h>
#include <QDebug> const static double PI = 3.1416; DrivedGraph::DrivedGraph()
{
m_bupFlag = true;
m_pixUp = QPixmap(":/Image/up.png");
m_pixDown = QPixmap(":/Image/down.png");
startTimer();
} void DrivedGraph::timerEvent(QTimerEvent *)
{
qreal edgex = scene()->sceneRect().right() + boundingRect().width()/; // 限定右边界
qreal edgetop = scene()->sceneRect().top() + boundingRect().height()/; // 上边界
qreal edgebottom = scene()->sceneRect().bottom() + boundingRect().height()/; // 下边界 qDebug() << "edgex: " << edgex << "edgetop: " << edgetop << "edgebottom: " << edgebottom; 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());
} qDebug() << "pos().x(): " << pos().x() << "pos().y(): " << pos().y() ; m_angle += (qrand()%) / 20.0;
qreal dx = fabs(sin(m_angle*PI)*10.0);
qreal dy = (qrand()%) - 10.0;
setPos(mapToParent(dx,dy));
} QRectF DrivedGraph::boundingRect() const
{
qreal adjust = ;
return QRectF(-m_pixUp.width()/ - adjust,-m_pixUp.height()/ - adjust,
m_pixUp.width() + adjust*,m_pixUp.height() + adjust*);
} void DrivedGraph::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
if (m_bupFlag) {
painter->drawPixmap(boundingRect().topLeft(),m_pixUp);
m_bupFlag = !m_bupFlag;
}
else {
painter->drawPixmap(boundingRect().topLeft(),m_pixDown);
m_bupFlag = !m_bupFlag;
}
}

实现蝴蝶飞舞

#include "butterfly.h"
#include <QApplication> #include "drivedgraph.h" int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Butterfly w;
// w.show(); QGraphicsScene *scene = new QGraphicsScene;
scene->setSceneRect(QRectF(-,-,,));
DrivedGraph *dri = new DrivedGraph;
dri->setPos(-,);
scene->addItem(dri); QGraphicsView *view = new QGraphicsView;
view->setScene(scene);
view->resize(,);
view->show(); return a.exec();
}

QGraphicsView,QGraphicsScene,QGraphicsItem的更多相关文章

  1. 第15.47节、PyQt显示部件:QGraphicsView图形视图和QGraphicsScene图形场景简介及应用案例

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一.概述 Designer中的Graphics V ...

  2. 杂烩:QWidget、QGraphics、QtQuick

    一说起Qt 大都会想起Qt的GUI编程,具体到某个类的话,最多的就是QWidget类及其子类了. 其实Qt中还有基于项的图形视图,具体来说QGraphicsView,QGraphicsScene,QG ...

  3. Qt之log数据展示模块简要实现

    Log模块主要用于实时测井数据的显示和测后曲线数据的预览和打印,为更好的展示对Qt中相关知识点的应用,特以Log模块为例对其进行简要实现. 内容导图: 一.功能需求 1.界面效果图 Log模块实现曲线 ...

  4. Qt Quick实现的涂鸦程序

    之前一直以为 Qt Quick 里 Canvas 才干够自绘.后来发觉不是,原来还有好几种方式都能够画图! 能够使用原始的 OpenGL(Qt Quick 使用 OpenGL 渲染).能够构造QSGN ...

  5. Graphics View框架

    Qt4.2开始引入了Graphics View框架用来取代Qt3中的Canvas模块,并在很多地方作了改进,Graphics View框架实现了模型-视图结构的图形管理,能对大量图元进行管理,支持碰撞 ...

  6. Qt快速入门学习笔记(画图篇)

    1.Qt中提供了强大的2D绘图系统,可以使用相同的API在屏幕和绘图设备上进行绘制,它主要基于QPainter.QPaintDevice和QPaintEngine这三个类.其中QPainter用来执行 ...

  7. Qt-MVC图形视图框架分解

    前面在<Qt-MVC图形视图框架出识>中我们了解了Qt图形视图框架中三个最基本的类,弄清他们的关系,本片小文,我们将对QGraphicsView,QGraphiceScene,QGraph ...

  8. 35.QT蝴蝶飞舞

    fly.h #ifndef FLY_H #define FLY_H #include <QObject> #include <QPainter> #include <QG ...

  9. Qt Examples - Boxes (在Qt场景视图中结合OpenGL渲染)

    QT自带例程Boxes使用QT Graphics View框架实现了2D图形和3D图形的混合渲染,综合性比较强,整合知识较多,值得学习. 可以使用鼠标通过以下方式控制演示中的元素: 按住鼠标左键的同时 ...

随机推荐

  1. Arm-Linux 移植 Nginx

    有关博客: <Windows 编译安装 nginx 服务器 + rtmp 模块>.<Ubuntu 编译安装 nginx>.<Arm-Linux 移植 Nginx> ...

  2. [洛谷P5323][BJOI2019]光线

    题目大意:有$n$层玻璃,每层玻璃会让$a\%$的光通过,并把$b\%$的光反射.有一束光从左向右射过,问多少的光可以透过这$n$层玻璃 题解:事实上会发现,可以把连续的几层玻璃合成一层玻璃,但是要注 ...

  3. 分享大麦UWP版本开发历程-03.GridView或ListView 滚动底部自动加载后续数据

    今天跟大家分享的是大麦UWP客户端,在分类.订单或是搜索时都用到的一个小技巧,技术粗糙大神勿喷. 以大麦分类举例,默认打开的时候,会为用户展示20条数据,当用户滚动鼠标或者使用手势将列表滑动到倒数第二 ...

  4. 【洛谷 P3224】 [HNOI2012]永无乡(Splay,启发式合并)

    题目链接 启发式合并就是暴力合并把小的合并到大的里,一个一个插进去. 并查集维护连通性,同时保证并查集的根就是所在Splay的根,这样能省去很多操作. #include <cstdio> ...

  5. 微信小程序 image组件坑

    远程图片 在真机上测试时 image组件只能显示http请求的图片, 对https 与 //xxx.xxx.xx 之类的不能显示. 可显示 'http://img.alicdn.com/i2/8323 ...

  6. 74.js---移动端文章的瀑布流的实现。

    移动端文章的瀑布流的实现.   1.首先在前端html页面已经通过PHP代码循环完全数据.  2.然后在js先全部隐藏,通过判断滑动到底部,每次加载一部分数据,直到数据全部显示完全. js代码: // ...

  7. 在safari下input的placeholder设置行高失效

    在项目中遇到input的placeholder在safari下设置行高失效的问题,亲测 input{ width:250px; height:30px; line-height:30px; font- ...

  8. antd-table——内容展示变型

    bug单: https://github.com/ant-design/ant-design/issues/13825 1.设置固定宽度:在columns中设置widht或者className { t ...

  9. robot framework笔记(三):扩展SeleniumLibrary库 (自定义关键字)

    (一)自定义和浏览器相关的关键字 以下代码GitHub 版本库地址: https://github.com/blairwind/blog_rf SeleniumLibrary的扩展文档中提供了3种增加 ...

  10. 小程序canvas绘制倒计时

    如果本文对你有用,请爱心点个赞,提高排名,帮助更多的人.谢谢大家!❤ 如果解决不了,可以在文末进群交流. 效果展示: //广告倒计时 advTimeCountDown:function(advTime ...