qgraphicsitem 复制副本_QGraphicsItem:调用paint函数时

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();

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/355176.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

怎么把两个div一左一右放

怎么把两个div一左一右放1.代码 <% page contentType"text/html;charsetUTF-8" language"java" %> <html> <head><title>Title</title> </head> <body> <div style"width:150px;height:50px;margin:0;…

为Java + STANDARD值引入Cucumber

作为软件开发人员&#xff0c;我们都有最喜欢的工具来使我们成功。 许多人在上手时就很适合这份工作&#xff0c;但很快就不见了。 其他人则需要太多的设置和培训才能“将脚趾浸入水中”&#xff0c;只是为了找出自己是否是正确的工具即可。 Cucumber JVM是一个测试框架&#…

一个逼格很低的appium自动化测试框架

Github地址: https://github.com/wuranxu 使用说明 1. 安装配置Mongo数据库 下载地址 mongo是用来存放元素定位的&#xff0c;截图如下: 通过case_id区分每个case的元素定位 里面提供了value, method和text字段&#xff0c;分别作用是定位的值&#xff0c;定位的方法和要输入的…

手机长曝光怎么设置_挑战黑暗:怎样用手机拍出漂亮的长曝光照片?

无数个血淋淋的教训告诉我们&#xff1a;用手机&#xff0c;玩夜拍真的不靠谱。在光线不足的情况下&#xff0c;手机那比指甲盖还小的传感器会产生可怕的噪点&#xff0c;不管你眼睛看到的夜景有多美&#xff0c;用手机拍出来&#xff0c;效果都会变得一团糟。难道夜晚真的是手…

Port already in use: 1099;

Port already in use: 1099;1.使用命令:netstat -aon|findstr 1099 找出占用1099端口的进程; 2.关闭占用该端口的进程:taskkill -f -pid 9336;

一款好用且免费的语句分析工具Plan Explorer

在调优过程中的查询语句优化阶段&#xff0c;分析语句的执行计划是必经之路&#xff0c;一款好的执行计划分析工具确实可以帮助我们事半功倍 小贴士&#xff1a;Plan Explorer是将Plan Explorer 专业版与免费版整合在一起发布的全新、完全免费版。微软的数据库专家和开发人员也…

前端如何发ModelAndView的请求

前端如何发ModelAndView的请求1.在Web.xml指定用作工具的跳转页面

带有Gluon Ignite和Dagger的JavaFX中的依赖注入

依赖注入抽象框架Gluon Ignite在几个流行的依赖注入框架&#xff08;例如Spring&#xff0c;Dagger和Guice&#xff09;上创建了一个通用抽象。 目前&#xff0c;Gluon 页面仅包含一个示例&#xff0c;该示例使用Gluon Ignite和Google Guice作为依赖注入框架&#xff0c;我想尝…

字符串逆置

重视思想和理解原理 #include <iostream> #include <cstdlib> #include <cstring> using namespace std;char* reverseStr(char * str, char * dest, int len) {//指向最后一个字符char * s &str[len-1];char *d dest;while(len > 0){*dest *s--;le…

友声电子秤设置软件_友声电子秤说明书/操作指南?(一)

友声电子秤说明书一、技术指标:1&#xff0e;内装免维护充电蓄电池&#xff0c;充电可同时开机使用。2&#xff0e;开机自动置零。3&#xff0e;零点自动跟踪。4&#xff0e;累计次数1~100次。5&#xff0e;电源&#xff1a;交流220V(10%、-10%)&#xff0c;50Hz&#xff0c;内…

checkbox对齐排列

checkbox对齐排列<span style"width: 120px;display: inline-block"><lable><input type"checkbox"/></lable> </span>常用的 display 可能的值&#xff1a;

c linux 获取cpuid_Linux下C编程 -- 得到系统的CPU信息(cpuid)

在 linux下可以通过查看 cat /proc/cpuinfo查看CPU的相关信息&#xff0c;但是在linux下C编程需要使用汇编语言来实现&#xff0c;因为C语言中没有实现查看CPU信息的函数&#xff0c;一般实现如下&#xff1a;(C中运行汇编用 __asm__等)#include#includestatic inline voidget_…

centos开发环境安装的备忘

#Centos visudo运行普通用户$(whomai)执行sudo操作 http://www.cnblogs.com/xianyunhe/archive/2011/08/08/2124342.html 在/etc/gdm/custom.conf文件中添加以下内容 [daemon] AutomaticLoginusername AutomaticLoginEnable…

动态增删表单

动态增删表单1.实现效果 2.两种方式&#xff0c;推荐第二种&#xff08;不管是第一种还是第二种&#xff0c;想要序列化都必须&#xff0c;id不同&#xff09; &#xff08;1&#xff09;表单clone的方式&#xff08;简单演示&#xff0c;道理相通&#xff09; <div id&quo…

策略模式(组件协作模式)

策略模式&#xff08;组件协作模式&#xff09; 策略模式实例代码 注解 目的 正常情况下&#xff0c;一个类/对象中会包含其所有可能会使用的内外方法&#xff0c;但是一般情况下&#xff0c;这些常使用的类都是由不同的父类继承、组合得来的&#xff0c;来实现代码的复用&…

openwrt 遍译php_openwrt安装编译

官网安装编译推荐&#xff1a;1. host32位主机~$uname -aLinux yuxi-T530 3.13.0-66-generic #108-Ubuntu SMP Wed Oct 7 15:21:40 UTC 2015 i686 i686 i686 GNU/Linux~$lsb_release -aNo LSB modules are available.Distributor ID: UbuntuDescription: Ubuntu14.04.3LTSRelea…

java11+osgi_错误学习:Java + OSGi

java11osgi最近&#xff0c;我致力于在OSGi环境中使Apache Hive工作。 虽然没有被证明是小菜一碟&#xff08;软件对吗&#xff1f;。。为什么我不感到惊讶&#xff1f; &#xff09;&#xff0c;它引导我解决了各种Java和OSGi错误。 在这里&#xff0c;我列出了其中一些让我有…

《Effective Java》读书笔记 - 5.泛型

Chapter 5 Generics Item 23: Don’t use raw types in new code 虽然你可以把一个List<String>传给一个List类型&#xff08;raw type&#xff09;的参数&#xff0c;但你不应该这么做&#xff08;因为允许这样只是为了兼容遗留代码&#xff09;&#xff0c;举个例子&am…

输入框长度校验

输入框长度校验1.例子 <input id"username" onkeyup"checkLength(this,80)"/>80是限制的长度 //输入长度校验,当长度超出限制&#xff0c;截取0到限制长度的字符串 function checkLength(obj, length) {if ($(# $(obj).attr("id")).val…

集群没有leader_ZooKeeper 集群中 Leader 与 Follower 的4种数据同步策略

首先要声明一点&#xff0c;zk集群中&#xff0c;leader服务器有着比较重要的存在&#xff0c;Follower 服务器只是处理非事务性请求&#xff0c;leader服务器主要负责事务性请求&#xff0c;Follower 服务器在遇到事务性请求以后还是会转发给leader服务器处理&#xff0c;所以…