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图形的混合渲染,综合性比较强,整合知识较多,值得学习. 可以使用鼠标通过以下方式控制演示中的元素: 按住鼠标左键的同时 ...
随机推荐
- Python Web开发技术栈
- L2R 一:基础知识介绍
一.背景 l2r可以说是搜索推荐里面很常用的知识了,一直处于一知半解的地步,今天开个博客准备把这些零散的东西系统性整理好,一版就粗糙点了. 二.粗概 前段时间的项目主要和搜索引擎相关,记录下搜索引擎的 ...
- 排Bug技巧
排Bug技巧 什么是Bug? Bug一词的英文原意是"臭虫"或"虫子".但是现在,在电脑系统或游戏程序中,如果隐藏着的一些未被发现的缺陷或问题,可被人利用,人们 ...
- 如何为非常不确定的行为(如并发)设计安全的 API,使用这些 API 时如何确保安全
原文:如何为非常不确定的行为(如并发)设计安全的 API,使用这些 API 时如何确保安全 .NET 中提供了一些线程安全的类型,如 ConcurrentDictionary<TKey, TVa ...
- Go语言的成功也预示着Rust的成功【转】
从整体的角度来看Go,很难理解他是怎么取得这么大的成功的.从理论的角度上来说Go是一门非常糟糕的语言,就算是和C++或者Ada之类旧语言相比也是这样. 从整体的角度来看Go,很难理解他是怎么取得这么大 ...
- GC偏好
GC偏好 测序中的GC偏好指的是基因组上GC含量在50%左右的区域更容易被测到,产生的reads更多,这些区域的覆盖度更高, 在高GC或者低GC区域,不容易被测到,产生较少的reads,这些区域的覆盖 ...
- Bootstraps 4 引入报错 Error: Bootstrap tooltips require Tether
问题: 解决办法 (http://github.hubspot.com/tether/) Bootstrap 4 needs Tether, so you need to include tether ...
- azkban从编译开始安装
从git上下载最新的azkban稳定版代码 git clone https://github.com/azkaban/azkaban.git -b 3.74.3 这里还有个问题,如何把这个代码放到自己 ...
- Libev库学习
Libev库学习 https://www.cnblogs.com/wunaozai/p/3950249.html Libev库学习(1)https://www.cnblogs.com/wunaozai ...
- SWD下载k60
转:JTAG各类接口针脚定义,含义及SWD接线方式 IAR设置如下