Skip to content

Qt/C++ State Machine of a Traffic Light

YAKINDU provides a Qt specific C++ code generator for statecharts. This example project shows how easy YAKINDU Qt/C++ state machines can be integrated into Qt applications.

The traffic light application

The example application is a simple traffic light with a red, yellow, and green bulb. the traffic light automatically switches between these colors. Additionally, there is a maintenance control that switches the traffic light from normal operation to attention mode and back. In attention mode the yellow light simply blinks.

The traffic lights application

Running the application

To get familiar with the example you may want to run the application. This Yakindu/Eclipse project includes a preconfigured Qt-Creator project including all required files. To run the included Qt application you require a Qt installation including its IDE. Once you have everything installed, open the Qt project file ./implementation/trafficlight-qt-cpp.pro in Qt-Creator. Then configure the project, build, and run it.

To run this example an installation of Qt is needed. It is recommended to use the latest version, but at least Qt 5.10.1 with Qt Creator 4.6.1. We also recommend to use MinGW 5.3.0 and CMake for building the project when using Windows.

To check the compiler configuration, navigate to 'Tools > Options ... > Build & Run > Compilers' in your Qt installation.

As soon as the application starts up you will see how the traffic light switches the colors starting with red. In order to switch the modes simply press the pause/play button at the bottom right corner.

The application logic

The behavior of the traffic light is defined by the single statechart (at model/TrafficLightStateMachine.sct).

The statechart

This statechart defines a set of in and out events that are used to interact with the application GUI. The out events like redOn or redOff are used to switch the traffic light colors. These out events will become Qt signals of the generated state machine class. The in events standBy or operate switch between the two main states operating and standby of the traffic light. These in events will become Qt slots of the generated state machine class. The different colors are switched within the entry and exit actions of the different states by raising the various out events. The statechart defines to be event-driven (see @EventDriven declaration). This implies that the in events instantly trigger state transitions if they are raised through a connected signal.

Integrating with Qt

As mentioned above the Qt specific flavor of the C++ code generator makes use of Qt signals and slots. So the generated state machine can easily be integrated into Qt applications using this mechanism. Beside the fact that the generated class inherits from QObject also a QTimer based implementation of a timer service is generated if the statechart makes use of after or every time triggers. The file implementation/main.cpp includes the code that is required to set up the a state machine instance and to connect it to the GUI. This user interface is implemented using a simple QML defintion (implementation/main.qml).

Generating state machine code

Feel free to play around with the the traffic light and its statechart. When you modify the statechart the code generator will automatically run and regenerate the sources that you can find in the implementation/machine folder. Also the synchronization between YAKINDU and Qt-Creator works painless. After generating the code, Qt-Creator may ask you to synchronize the new versions of the implementation files. Simply confirm and rebuild the project - that's it.

Installing the Qt/C++ code generator

If code generation does not work and YAKINDU shows error markers on the model/TrafficLightStateMachine.sgen file then you likely have to install the YAKINDU Qt/C++ code generator.

  1. Open Help > Install New Software ... from the menu.
  2. Select the Yakindu Labs entry from the Work with: pull down list. If there is no such entry then
    1. Choose Add
    2. Enter http://updates.yakindu.com/statecharts/labs in the Location field and optionally choose a name.
    3. Press OK.
  3. Unfold the top level node and check the Qt/C++ code generator feature.
  4. Finish the installation wizard by choosing Next > until finished.
  5. Restart.

Download examples

Drag to install

Drag to your running itemis CREATE (min. version 2.8.0) workspace.

Back to overview