State machines in Qt 6
A state machine can be used for many purposes but, in this chapter, we will only cover topics related to animation.
How to do it…
A State machine is not that hard to achieve in Qt at all. Let’s get started by following these steps:
- We will set up a new user interface for our example program, which looks like this:

Figure 3.5 – Setting up the GUI for our state machine experiment
- We will include some headers in our source code:
#include <QStateMachine> #include <QPropertyAnimation> #include <QEventTransition>
- In our main window’s constructor, add the following code to create a new state machine and two states, which we will be using later:
QStateMachine *machine = new QStateMachine(this); QState *s1 = new QState(); QState *s2 = new QState();
- We will define what we should do within each state, which, in this case, will be to change the label’s text and the...