QGraphicsView,QGraphicsScene,QGraphicsItem
参考: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的更多相关文章
- 第15.47节、PyQt显示部件:QGraphicsView图形视图和QGraphicsScene图形场景简介及应用案例
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一.概述 Designer中的Graphics V ...
- 杂烩:QWidget、QGraphics、QtQuick
一说起Qt 大都会想起Qt的GUI编程,具体到某个类的话,最多的就是QWidget类及其子类了. 其实Qt中还有基于项的图形视图,具体来说QGraphicsView,QGraphicsScene,QG ...
- Qt之log数据展示模块简要实现
Log模块主要用于实时测井数据的显示和测后曲线数据的预览和打印,为更好的展示对Qt中相关知识点的应用,特以Log模块为例对其进行简要实现. 内容导图: 一.功能需求 1.界面效果图 Log模块实现曲线 ...
- Qt Quick实现的涂鸦程序
之前一直以为 Qt Quick 里 Canvas 才干够自绘.后来发觉不是,原来还有好几种方式都能够画图! 能够使用原始的 OpenGL(Qt Quick 使用 OpenGL 渲染).能够构造QSGN ...
- Graphics View框架
Qt4.2开始引入了Graphics View框架用来取代Qt3中的Canvas模块,并在很多地方作了改进,Graphics View框架实现了模型-视图结构的图形管理,能对大量图元进行管理,支持碰撞 ...
- Qt快速入门学习笔记(画图篇)
1.Qt中提供了强大的2D绘图系统,可以使用相同的API在屏幕和绘图设备上进行绘制,它主要基于QPainter.QPaintDevice和QPaintEngine这三个类.其中QPainter用来执行 ...
- Qt-MVC图形视图框架分解
前面在<Qt-MVC图形视图框架出识>中我们了解了Qt图形视图框架中三个最基本的类,弄清他们的关系,本片小文,我们将对QGraphicsView,QGraphiceScene,QGraph ...
- 35.QT蝴蝶飞舞
fly.h #ifndef FLY_H #define FLY_H #include <QObject> #include <QPainter> #include <QG ...
- Qt Examples - Boxes (在Qt场景视图中结合OpenGL渲染)
QT自带例程Boxes使用QT Graphics View框架实现了2D图形和3D图形的混合渲染,综合性比较强,整合知识较多,值得学习. 可以使用鼠标通过以下方式控制演示中的元素: 按住鼠标左键的同时 ...
随机推荐
- Nginx 配置 HTTP 跳转 HTTPS-Linux运维日志
本文介绍 Nginx 访问 HTTP 跳转 HTTPS 的 4 种配置方式. rewrite Nginx rewrite 有四种 flag: break:在一个请求处理过程中将原来的 url 改写之后 ...
- python 操作redis集群
一.连接redis集群 python的redis库是不支持集群操作的,推荐库:redis-py-cluster,一直在维护.还有一个rediscluster库,看GitHub上已经很久没更新了. 安装 ...
- 访问Harbor报502 Bad Gateway
Harbor启动都是多个容器的,首先查看一下是否有相关容器未启动 docker ps | grep harbor cae340214e57 goharbor/nginx-photon:v1.9.3 & ...
- wbSocket
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 数据结构与算法(周测7-拓扑排序和AOV网络)
判断题 1.AOE图的关键路径就是最长的路径 T F 2.AOE图的权值最大的边(活动)一定是关键活动. T F 两条边相加可能比最大的边还要大. 3.在AOE ...
- 【开发笔记】- Java读取properties文件的五种方式
原文地址:https://www.cnblogs.com/hafiz/p/5876243.html 一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供j ...
- input中blur失去焦点事件与点击事件冲突时如何解决
方法一 使用setTimeout $(function(){ $(".cy-name-input input").on({ focus:function() { $(". ...
- Charles中文破解版下载安装及使用教程(附带免费下载链接)
一. 简介及安装 Charles 是在 PC 端常用的网络封包截取工具,但它不仅仅能在pc端使用,还可以在手机ios和安卓端都可以使用.我们在做移动开发或者测试网页app时候,为了调试与服务器端的网络 ...
- Linux下MySQL的数据文件存放在哪里的??
http://bbs.csdn.net/topics/390620630 mysql> show variables like '%dir%';+------------------------ ...
- 2.kafka 分布式集群安装
Kafka集群安装主节点h201,从节点h202.h2031.安装jdk1.8[hadoop@h201 ~]$ /usr/jdk1.8.0_144/bin/java -version 2.安装zook ...