Putting a Spin on Things

Today's example shows how to reuse the statechart from the flippin' widgets example to create a widget that spins. The idea is to create a spin state that repeatedly enters a flip state until the speed is sufficiently slow (i.e. the widget should stop spinning). The spin state doesn't know the internals of the flip state; the only thing it needs to do is enter the flip state, and have a state transition that's triggered by the flip state's finished() signal (at which point a single flip has been performed). In other words, the SpinWidget "embeds" the FlipWidget's statechart in its own and builds more behavior on top. The spin state emits the finished() signal when its work is done, so now we can build new behavior on top of that state, and so on; indeed, the top-level states of the SpinWidget's statechart are simply an idle state and a spin state. Also worth noting is that all states are composed of standard QState objects (no subclassing needed).

For fun I threw in a "pulse" effect for the item under the cursor. Video:

Download the example code here (qt/master required).

Blog Topics: