CREATE provides a Qt specific C++ code generator for statecharts. This example project shows how easy CREATE Qt/C++ state machines can be integrated into Qt applications.
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.
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 behavior of the traffic light is defined by the single statechart (at model/TrafficLightStateMachine.sct).
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.
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).
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 CREATE 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.
If code generation does not work and CREATE shows error markers on the model/TrafficLightStateMachine.sgen file then you likely have to install the CREATE Qt/C++ code generator.