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

The Animation framework

For now, we have calculated and applied new positions for our graphics items manually. However, Qt provides a way to do it automatically, called the Animation framework.

The framework is an abstract implementation of animations, so it can be applied to any QObject, such as widgets, or even plain variables. Graphics, items can be animated too, and we will get to this topic soon. Animations are not restricted to the object's coordinates. You can animate color, opacity, or a completely invisible property.

To create an animation, you typically need to perform the following steps:

  1. Create an animation object (such as QPropertyAnimation)
  2. Set the object that should be animated
  3. Set the name of the property to be animated
  4. Define how exactly the value should change (for example, set starting and ending values)
  5. Start the animation

As you probably know, calling an arbitrary method by name is not possible in C++, and yet, the animation objects are able to change arbitrary properties at will. This is possible because "property" is not only a fancy name, but also another powerful feature of the QObject class and Qt meta-object compiler.