State machines in Qt
State machines can be used for many purposes, but in this chapter we will only cover topics related to animation.
How to do it…
- First, we will set up a new user interface for our example program, which looks like this:

- Next, we will include some headers in our source code:
#include <QStateMachine> #include <QPropertyAnimation> #include <QEventTransition>
- After that, 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();
- Then, we will define what we should do within each state, which in this case will be to change the label's text, as well as the button's position and size:
QState *s1 = new QState(); s1->assignProperty(ui->stateLabel, "text", "Current state: 1"); s1->assignProperty(ui->pushButton, "geometry", QRect...
You have been reading a chapter from
Qt5 C++ GUI Programming Cookbook
Published in:
Jul 2016
Publisher:
Packt
ISBN-13:
9781783280278
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime