I am creating an animation with QGraphicsView, QGraphicsScene and QGraphicsItem. Can someone explain me when the paint function is called? Although I does not change variables of the item, the paint function is called every ≈100 ms. Can I stop that, so i can repaint the item when I want?

解决方案

You are approaching it the wrong way. The item should be repainted only when needed - when you change how it looks or where it's located. That's when you call the QGraphicsItem::update(). The rest will be handled for you. It seems you're overcomplicating things.

Do note that you need to be determining the current time-dependent parameter of the animation within the paint() method, or "close" to it (say, right before update() is called), using actual time! If your animations are derived from QAbstractAnimation, it's already done for you. If they are not, then you'll have to use QElapsedTimer yourself.

The relevant Qt documentation says:

The animation framework calls updateCurrentTime() when current time has changed. By reimplementing this function, you can track the animation progress. Note that neither the interval between calls nor the number of calls to this function are defined; though, it will normally be 60 updates per second.

This means that Qt will do animations on a best-effort basis. The currentTime reported by the animation is the most recent time snapshot at the moment the animation was updated in the event loop. This is pretty much what you want.

The simplest way to deal with all this would be to use QVariantAnimation with QGraphicsObject. An example is below. Instead of rotating the object, you may have your own slot and modify it in some other way. You can also, instead of using signal-slot connection, have a customized QVariantAnimation that takes your custom QGraphicsItem-derived class as a target.

main.cpp

#include

#include

#include

#include

#include

#include

class EmptyGraphicsObject : public QGraphicsObject

{

public:

EmptyGraphicsObject() {}

QRectF boundingRect() const { return QRectF(0, 0, 0, 0); }

void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) {}

};

class View : public QGraphicsView

{

public:

View(QGraphicsScene *scene, QWidget *parent = 0) : QGraphicsView(scene, parent) {

setRenderHint(QPainter::Antialiasing);

}

void resizeEvent(QResizeEvent *) {

fitInView(-2, -2, 4, 4, Qt::KeepAspectRatio);

}

};

void setupScene(QGraphicsScene &s)

{

QGraphicsObject * obj = new EmptyGraphicsObject;

QGraphicsRectItem * rect = new QGraphicsRectItem(-1, 0.3, 2, 0.3, obj);

QPropertyAnimation * anim = new QPropertyAnimation(obj, "rotation", &s);

s.addItem(obj);

rect->setPen(QPen(Qt::darkBlue, 0.1));

anim->setDuration(2000);

anim->setStartValue(0);

anim->setEndValue(360);

anim->setEasingCurve(QEasingCurve::InBounce);

anim->setLoopCount(-1);

anim->start();

}

int main(int argc, char *argv[])

{

QApplication a(argc, argv);

QGraphicsScene s;

setupScene(s);

View v(&s);

v.show();

return a.exec();

}

qgraphicsitem 复制副本_QGraphicsItem:调用paint函数时相关推荐

  1. qgraphicsitem 复制副本,QGraphicsItem:调用paint函数时

    I am creating an animation with QGraphicsView, QGraphicsScene and QGraphicsItem. Can someone explain ...

  2. python/由于keys重复导致字典dict在调用update函数时被覆盖

    一.连接两个dict aaa = {'question_type': 'What is the overall condition of the given image?', 'multiple_ch ...

  3. 卷影复制服务错误: 调用程序 CoCreateInstance 时的意外错误。hr = 0x8007045b, 系统正在关机。

    服务器有时自动重启: 卷影复制服务错误: 调用程序 CoCreateInstance 时的意外错误.hr = 0x8007045b, 系统正在关机. 在服务中是:Volume Shadow Copy和 ...

  4. GridView调用setAdapter()函数时发生错误

    程序在执行gridview.setAdapter(adapter);时发生错误,其中gridview为GridView类型,adapter是继承了BaseAdap类的一个对象.错误信息如下: Fina ...

  5. Intel Realsense D435 是否启动pipeline.start()就开始传输帧,还是只有我们调用wait_for_frames()函数时它才会传输帧?(任务管理器USB带宽内存测试)

    我猜想Intel Realsense D435摄像头只要启动pipeline.start()函数,就会开始传输帧,无论我们是否调用wait_for_frames()或者poll_for_frames( ...

  6. qgraphicsitem 复制副本_如何在pyqt5中复制粘贴Qgraphicsitem?

    在场景中复制粘贴QGraphicsitem时遇到问题. 我尝试了以下代码,但它不能正常工作. 如果我尝试粘贴项目,第一个实例就是正确粘贴.对于第二个实例,它删除第一个实例项并粘贴第二个实例.在 到目前 ...

  7. python rpy2时间序列_当从多线程使用rpy2调用r函数时,模型作为r函数的参数

    如果我理解正确,你:有一个用R编写的函数(分类器),它需要一个相对大的数据体才能工作(k个最近的邻居?)在 正在使用Python加载该数据体 希望加载参数/once/,然后根据需要对分类器进行尽可能多 ...

  8. 关于64位MATLAB调用refprop函数时出错的解决方法

    注:本人refprop的安装文件夹为C:\Program Files\REFPROP(如有差异,在自己refprop的安装文件夹中进行操作).电脑系统需为64位. 使用matlabR2021a和REF ...

  9. python调用子函数时参数传递问题

    场景: python建立子函数,在主程序中进行调用. 问题描述 将全局变量传入子函数,子函数中对局部变量进行修改导致全局变量发生. 示例代码: import numpy as np def softm ...

最新文章

  1. LeetCode刷题-2
  2. spring中lazy-init详解
  3. 【数据竞赛】从0梳理1场数据挖掘赛事!
  4. C++什么时候需要使用“常引用”?
  5. 历史需要重写?AlexNet之前,早有算法完成计算机视觉四大挑战
  6. python中classmethod与staticmethod的差异及应用
  7. 超详细 | 接口自动化测试总结与分享入门篇
  8. keil p0 0c语言不了,Keil C51对C语言的关键词扩展之十三: sfr
  9. 财子说丨王国斌:相信时间的力量
  10. cadence入门学习
  11. c语言中分号作用,在C语言中逗号、分号等的用法
  12. vivo手机光环助手下载游戏怎么打不开_“点亮夜色,点亮你”——全新5G自拍手机,vivo S6系列正式发布...
  13. find:paths must precede expression问题及解决
  14. Apache Spark 3.0 DStreams-Streaming编程指南
  15. 日渐临近的苹果秋季发布会,iOS 11 GM 固件到底提前泄露了哪些秘密?
  16. 计算机研究生论文期刊,经典计算机研究生小论文参考文献 计算机研究生小核心期刊参考文献哪里找...
  17. 华为手表开发:WATCH 3 Pro(17)传感器订阅指南针
  18. 火了70年的厨电界“好莱坞大明星”,各种影视剧都用它当道具来提气丨钛空最生活...
  19. 技术小品文(一)字符串放在哪里?
  20. 对高考选志愿的一点看法

热门文章

  1. 【渝粤教育】电大中专电大中专沟通技巧考试考核试题 (2)作业 题库
  2. 【渝粤教育】电大中专电子商务网站建设与维护 (21)作业 题库
  3. 【渝粤教育】广东开放大学 javaweb 动态网站开发技术 形成性考核 (53)
  4. 1G、3G都失败了,5G也会失败吗?
  5. 新容器!不再担心空指针, Java8 Optional类
  6. Java面向对象(10)--super关键字
  7. php mysql 随机字符串_MySQL_Mysql 自定义随机字符串的实现方法,前几天在开发一个系统,需要 - phpStudy...
  8. CDMA系统的三种码
  9. 工程计价里面的表以及相关税额的计算
  10. 常见的股票技术因子学习以及计算