Skip to content

[Pro] Traffic Light (SCXML) for QT

This is a simple example for the Qt SCXML integration of CREATE Statechart Tools. The example is inspired by the traffic light examples that can be found here. The example uses a manually made statechart model from which a scxml-file is generated, which then can be executed as a standalone application within the QT environment. The manually made statechart model looks as follows:

The traffic light statechart

The main part of the statechart consists of two composite states "working" and "broken". Each of the states has several substates for the different modes of a traffic light.

Overview

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.

The example project contains several files which declare the behaviour and look-and-feel of the traffic light:

  • Button.qml - Declares the trigger for break/repair the traffic light
  • Lights.ui.qml - Declares how the traffic light is displayed when executing
  • TrafficLight.qml - Declares which components should be used for displaying the traffic light within an application window
  • TrafficLightStateMachine.scxml - The statemachine generated as sccxml-file from the reference model
  • trafficlight-project.qrc - The resources file which references images for displaying the traffic light
  • trafficlight-project.pro - The Qt project file that is needed for execution
  • several images
  • trafficlight (.h, .cpp) - Mapping between the trafficlight statemachine and user interface
  • trafficlight-main (.cpp) - Implementation of the main application

Set up the project

To setup the trafficlight project you will first need to generate the .scxml statemachine file.

  • Right-click on 'model/model.sgen' in the traffic_light_qt_scxml project in the 'Project Explorer'
  • Choose 'Generate Code Artifacts'
  • The 'TrafficLightStateMachine.scxml' should be created in the 'src-gen' folder.

Load the project

  • Select 'Open Project' in the welcome page
  • Navigate to the folder 'src' within the trafficlight example project
  • Choose 'trafficlight-project.pro'
  • Click 'Open'
  • The project should be opened and build automatically.

Build the project manually

  • Right-click on project 'trafficlight-project' in the Qt projects view
  • Select 'Run qmake'
  • Wait for completion
  • Right-click again on project
  • Select 'Build'
  • Wait for completion

Run application

  • Right-click again on project 'trafficlight-project' in the Qt projects view
  • Select 'Run'
  • The traffic light application should be started. A traffic light which consists of a red, yellow and green bulb is displayed and is switching between the states. The traffic light can be set to broken/repaired by clicking on the 'Pause/Play' button at the bottom right, see the following image: Traffic light

Run Qt Unit tests

You can run Qt Unit tests to test the traffic light statemachine behaviour. For this Itemis CREATE provides a SCTUnit code generator which generates a whole Qt Unit test project.

  • Right-click on 'QtSCXMLTests.sgen' in project folder traffic_light_qt_scxml in 'Project Explorer' of CREATE Statechart Tools
  • The folder 'test-gen' should be created containing the QtSCTUnit project with a reference to the statechart defined in 'TrafficLightStateMachine.scxml' which can be loaded into Qt.

Run statechart simulation

Additionally you can simulate the statechart in CREATE Statechart Tools by right-click on the 'TrafficLightStateMachine.sct' file and choose:

  • 'Run As > Statechart Simulation'

You'll find more information on using the simulation in the help.

Download examples

Drag to install

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

Back to overview