日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/2 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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();

}

總結

以上是生活随笔為你收集整理的qgraphicsitem 复制副本_QGraphicsItem:调用paint函数时的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。