Game Programming using Qt 5 Beginner's Guide
上QQ阅读APP看书,第一时间看更新

A third way of animation

Besides QTimer and QPropertyAnimation, there is a third way to animate the scene. The scene provides a slot called advance(). If you call that slot, the scene will forward that call to all items it holds by calling advance() on each one. The scene does that twice. First, all item advance() functions are called with 0 as an argument. This means that the items are about to advance. Then, in the second round, all items are called passing 1 to the item's advance() function. In that phase, each item should advance, whatever that means—maybe moving, maybe a color change, and so on. The scene's slot advance is typically called by a QTimeLine element; with this, you can define how many times during a specific period of time the timeline should be triggered.

QTimeLine *timeLine = new QTimeLine(5000, this);
timeLine->setFrameRange(0, 10); 

This timeline will emit the frameChanged() signal every 5 seconds for 10 times. All you have to do is connect that signal to the scene's advance() slot, and the scene will advance 10 times in 50 seconds. However, since all items receive two calls for each advance, this may not be the best animation solution for scenes with a lot of items where only a few should advance.