Archived:How to create a 2-state button using the Qt State Machine Framework
This example shows how to use the Qt State Machine Framework to implement a simple state machine that toggles the current state when a button is clicked.
- construct a button and a state machine
- Create two states: on and off. assign Property and ObjectName of the each state
QState *off = new QState();
off->assignProperty(&button, "text", "Off");
QState *on = new QState();
on->assignProperty(&button, "text", "On");
- When the state machine is in the off state and the button is clicked, it will transition to the on state; when the state machine is in the on state and the button is clicked, it will transition to the off state.
off->addTransition(&button, SIGNAL(clicked()), on);
on->addTransition(&button, SIGNAL(clicked()), off);
- add states to the state machine
- set the Initial State
- show the button