How To Create State Machine In Qt5 GUI

In this article we are going to talk about How To Create State Machine In Qt5 GUI, State Machines can be used for many purposes, but in this article we will only cover topics
related to animation.

 

First, we will set up a new user interface for our example program, which looks like this, also you need to change the name of label to stateLabel

and Change State PushButton to changeState, and also PushButton to pushButton.

Qt5 StateMachine Design
Qt5 StateMachine Design

 

 

Next, we will include some headers in our source code

 

 

After adding your mainwindow.h will look like this

 

 

 

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.

 

 

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.

 

 

Now we are going to create our animations

 

 

Once you are done with that, let’s proceed by adding event transition classes to our source code.

 

Next, add all the states we have just created to the state machine and define state 1
as the initial state. Then, call machine->start() to start running the state machine

 

 

How it Works

There are two push buttons and a label on the main window layout. The button at the top-
left corner will trigger the state change when pressed, while the label at the top-right corner
will change its text to show which state we are currently in, and the button below will
animate according to the current state.

The QEventTransition classes define what will trigger the transition between one state and
another.

In our case, we want the state to change from state 1 to state 2 when the ui->changeState
button (the one at the upper left) is clicked. After that, we also want to change from state 2
back to state 1 when the same button is pressed again. This can be achieved by creating
another event transition class and setting the target state back to state 1. Then, add these
transitions to their respective states.

Instead of just assigning the properties directly to the widgets, we tell Qt to use the
property animation class to smoothly interpolate the properties toward the target values. It
is that simple!
There is no need to set the start value and end value, because we have already called the
assignProperty() function, which has automatically assigned the end value.

 

 

Also check Qt5 C++ GUI Development Articles in the below links

1: Qt5 C++ Introduction And Installation

2: Qt5 C++ First Console Application 

3: Qt5 C++ First GUI Application 

4: Qt5 C++ Signal And Slots Introduction

5: Qt5 C++ Layout Management 

6: Qt5 C++ Creating Qt Style Sheets

7: Qt5 C++ Creating QPushButton

8: How To Create QCheckBox in Qt5

9: Qt5 GUI How To Create QRadioButton

10: Qt5 GUI Development How To Create ComboBox

11: Qt5 C++ GUI Development Creating QListWidget

12: Qt5 C++ GUI Development Creating QMessageBox

13 : Qt5 C++ GUI Creating QMenu And QToolbar

14: Qt5 C++ GUI Development Creating QPrintDialog

15: Qt5 C++ GUI Development Creating QFontDialog

16: Qt5 C++ GUI Development Creating QColorDialog

17: How to Create QFileDialog in Qt5 C++

18: How to Create QProgressbar in Qt5 C++

19: How to Create QPropertyAnimation in Qt5

20: How To Control QPropertyAnimation in Qt5 

21: How To Create AnimationGroup in Qt5

 

So now run the complete project , after clicking the Change State button you will see the state changing  

How To Create State Machine In Qt5 GUI
How To Create State Machine In Qt5 GUI

 

 

 

Also you can watch the complete video for this article 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×