Solved OnEntry/OnExit/OnTransition signals for SCXML
-
I have an application using an SCXML
StateMachine
. I want to receive signals in QML when I enter or exit particular states, or (stretch goal) when certain transitions are executed.According to this post I can emit a custom signal like so:
<onentry><send type="qt:signal" event="enteredFoo"/></onentry>
However, I cannot figure out how to actually connect to such a signal in QML. For example, the code at bottom (unsurprisingly) produces the error:
TypeError: Cannot call method 'connect' of undefined
How can I detect when the state machine enters or exits a state?
import QtQuick 2.7 import QtScxml 5.8 Window { visible: true property StateMachine logic: logicLoader.stateMachine StateMachineLoader { id:logicLoader; source:"main.scxml" } Component.onCompleted: { logic.enteredFoo.connect(function(){ console.log('w000t'); }); } }
-
Of course the answer is RTFM :)
The documentation on state machines mentions the
EventConnection
type. This emits a singleoccurred
signal when specific, listed SCXML events occur (fired without thetype="qt:signal"
attribute), and allows patterns like:property StateMachine logic: logicLoader.stateMachine StateMachineLoader { id:logicLoader; source:"main.scxml" } EventConnection { signal enteredFoo(var event) signal enteredBar(var event) stateMachine: logic events: ["enteredFoo", "enteredBar"] onOccurred: this[event.name](event); }
This requires you to manually enter the enter/exit/transition event three times:
- In the SCXML as a
send
action - In the
EventConnection
as a signal - In the
events
list in theEventConnection
...but it does work!
Hey Qt! Please add a better way to do this! :)
- In the SCXML as a
-
Of course the answer is RTFM :)
The documentation on state machines mentions the
EventConnection
type. This emits a singleoccurred
signal when specific, listed SCXML events occur (fired without thetype="qt:signal"
attribute), and allows patterns like:property StateMachine logic: logicLoader.stateMachine StateMachineLoader { id:logicLoader; source:"main.scxml" } EventConnection { signal enteredFoo(var event) signal enteredBar(var event) stateMachine: logic events: ["enteredFoo", "enteredBar"] onOccurred: this[event.name](event); }
This requires you to manually enter the enter/exit/transition event three times:
- In the SCXML as a
send
action - In the
EventConnection
as a signal - In the
events
list in theEventConnection
...but it does work!
Hey Qt! Please add a better way to do this! :)
- In the SCXML as a