Imagine a part of your state machine looks like this:
How do you properly implement the choice part in Qt? I know there are guarded transitions, but that would mean that I need to:
- Create a subclass of a QAbstractTransition which accepts e.g. an
std::function<bool()>
and a flag which determines if the transition happens when that boolean result is true, or when it is false - Create two instances of this class with the same boolean function, but opposite transition guards
- Add two transitions from
S1
using these two instances.
That approach seems kind of clumsy and error prone for something as simple as a choice. Is there a more maintainable approach to implement this?
License Notice:
Alternatively to the default StackOverflow license you are hereby allowed to use this code through the MIT License.
I've created a
BooleanChoiceTransition
class with a constructor like this (might contain errors, the code is not on this machine, so I typed it by heart):with
transitionToTrueTarget
andtransitionToFalseTarget
being signals of course.For the case of the example in the question, the class can be used like so:
Since
BooleanChoiceTransition
is aQState
, this can even be nested easily: