参考: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. MySQL运维中的Tips--持续更新

    1.into outfile 生成sql:一般都是生成文本或者其他形式的文件,现在需要生成sql形式的文件.配置文件加secure_file_priv=''select concat('insert ...

  2. Badboy与Jmeter脚本录制

    下载地址: http://www.badboy.com.au         Badboy 是一个强大的工具,旨在帮助测试和开发复杂的动态应用,Badboy 包括一个简单而全面的捕获/回放界面,强大的 ...

  3. [高清·非影印] 深度学习入门:基于Python的理论与实现 + 源代码

    ------ 郑重声明 --------- 资源来自网络,纯粹共享交流, 如果喜欢,请您务必支持正版!! --------------------------------------------- 下 ...

  4. org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): 错误解决

    报错信息:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 说明:这段报错信息表示 Map ...

  5. C#特性的学习(一)

    1.预定定义特性之一:AttributeUsage AttributeUsage有三个属性: 第一个属性:ValidOn 规定特性可被放置的语言元素,默认是AttributeTargets.All.

  6. 父元素设置min-height子元素设置100%问题

    问题:父元素设置min-height子元素高度设置100%取不到值,这是因为子元素 div设置 height:100%: 只有当父级元素满足min-height:1000px;设置的条件才触发: 浏览 ...

  7. 2019北航OO第三单元作业总结

    1.梳理JML语言的理论基础.应用工具链情况 JML基础理论: JML(Java Modeling Language)是用于对Java程序进行规格化设计的一种表示语言.JML是一种行为接口规格语言,基 ...

  8. Kubernetes 1.15部署日记-使用kubeadm--<7-8>

    7. 在k8s集群中run一个应用 nginx已经跑起来了到此kubernetes集群部署结束了. 8. 总结 此次部署kubernetes的起因是AI团队中的kubernetes项目跑在其他IP段网 ...

  9. obj + mtl 格式说明

    OBJ(或 .OBJ)是一种开放的几何定义文件格式,最初由Wavefront Technologies公司开发,用以描述其Advanced Visualizer动画包.该格式已被其他3D图形应用供应商 ...

  10. Springboot手动获取bean

    使用如下工具类即可 package com.rio.ums.spa.commons.utils; import org.springframework.beans.BeansException; im ...