Question

I would like paintEvent() to be called 60 times per second to move a several objects inside a small widgets. Widget is similar to chrome browser's tab: when you drag a tab far away from its position it moves back, reducing speed as it arrives to its place.

I use QTimer connected to a slot where i recalculate some geometric parameters and then call update() to make qt main loop call paintEvent() where i draw some lines and QPixmaps.

But two QTimer objects set for 1/60 sec interval are consuming 10% of "Core i5 2500K" CPU. Isn't that too much CPU time for 2 timers with 60 ticks per second each? The situation with CPU time is the same if i remove all the code from the slot() the QTimer is connected to (except "return;" statement).

Was it helpful?

Solution

Normally for animations, you let Qt figure out the appropriate timing. Is there a particular reason to run it so fast? If you use QPropertyAnimation, you can get some pretty slick results, with smooth animations. The EasingCurves also can give you some great effects.

http://qt.apidoc.info/4.8.5/animation-animatedtiles.html

EDIT: Also calling update 60x a second doesn't necessarily redraw it 60 times a second; instead you are putting requests-for-a-repaint on the event loop 60x a second. Depending on how much time Qt gets and the refresh rate of the graphics card and what not, if the main GUI event loop of your program may only get around to redrawing your graphic 20-35x a second, the multiple update calls get ignored. If you demand to be drawn 60x a second you would use repaint(). But in most cases you should call update.

http://qt-project.org/doc/qt-4.8/qwidget.html#update

Also you should try to cache as much of the calculations as you can if it is slowing your computer down so much. Hope that helps.

OTHER TIPS

Wait, i think there is something wrong with design. Can you pls clarify what you actually trying to achieve? Either 'drag' some widget content and move it back on mouseRelease (for example) or trying to drag away let's say page from tab widget and also move it slowly back?

I am asking because if you need to move widget around you don't need to call paintEvent 60 times a second, you need to work around widget geometry and position, surely not call an update() yourself, OR if you are talking about widget content then you should look on subclassing QGraphicsEffect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top