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?
相关问题
- QML: Cannot read property 'xxx' of undefin
- QTextEdit.find() doesn't work in Python
- QT Layouts, how to make widgets in horizontal layo
- QT GUI freezes even though Im running in separate
- Direct2D Only Partially Linking in C++ Builder
相关文章
- ubuntu20.4中c#通过c++库调用python脚本
- Qt槽函数自动执行多遍
- Is there a non-java, cross platform way to launch
- How to get a settings storage path in a cross-plat
- Why doesn't valgrind detect a memory leak in m
- Behavior of uniforms after glUseProgram() and spee
- QTreeView remove decoration/expand button for all
- qt界面拥挤
You can set the viewportUpdateMode of the QGraphicsView to change how it updates. The options are: -
The Qt docs explains what the different options do, but if you want full control, just set to QGraphicsView::NoViewportUpdate and control it yourself using a QTimer event.
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 beforeupdate()
is called), using actual time! If your animations are derived fromQAbstractAnimation
, it's already done for you. If they are not, then you'll have to useQElapsedTimer
yourself.The relevant Qt documentation says:
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
withQGraphicsObject
. 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 customizedQVariantAnimation
that takes your customQGraphicsItem
-derived class as a target.main.cpp